最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React JS reference function in another component - Stack Overflow

programmeradmin5浏览0评论

I'm trying to get a button rendered through another ponent to reference and/or influence the state of a different ponent.

var Inputs = React.createClass({
  getInitialState: function(){
    return {count: 1};
  },
  add: function(){
    this.setState({
      count: this.state.count + 1
    });
  },
  render: function(){
    var items = [];
    var inputs;
      for (var i = 0; i < this.state.count; i++){
        items.push(<input type="text" name={[i]} />);
        items.push(<br />);
      }
    return (
      <div className="col-md-9">
        <form action="/" method="post" name="form1">
          {items}
          <input type="submit" className="btn btn-success" value="Submit Form" />
        </form>
      </div>
     );
   }
 });

I want to write a new ponent that will be able to access the add function in Inputs. I tried to reference it directly with Inputs.add like this:

var Add = React.createClass({
  render: function(){
    return (
      <input type="button" className="btn" value="Add an Input" onClick={Inputs.add} />
    );
  }
});

But that didn't work. How would I be able to access a ponent's functions through another ponent, or influence the state of a ponent through another ponent? Thanks.

I'm trying to get a button rendered through another ponent to reference and/or influence the state of a different ponent.

var Inputs = React.createClass({
  getInitialState: function(){
    return {count: 1};
  },
  add: function(){
    this.setState({
      count: this.state.count + 1
    });
  },
  render: function(){
    var items = [];
    var inputs;
      for (var i = 0; i < this.state.count; i++){
        items.push(<input type="text" name={[i]} />);
        items.push(<br />);
      }
    return (
      <div className="col-md-9">
        <form action="/" method="post" name="form1">
          {items}
          <input type="submit" className="btn btn-success" value="Submit Form" />
        </form>
      </div>
     );
   }
 });

I want to write a new ponent that will be able to access the add function in Inputs. I tried to reference it directly with Inputs.add like this:

var Add = React.createClass({
  render: function(){
    return (
      <input type="button" className="btn" value="Add an Input" onClick={Inputs.add} />
    );
  }
});

But that didn't work. How would I be able to access a ponent's functions through another ponent, or influence the state of a ponent through another ponent? Thanks.

Share Improve this question asked Jan 10, 2014 at 21:43 user2829448user2829448 1531 gold badge2 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

You could acplish this by creating a parent ponent that is responsible for managing the state and then just push the state down to the sub-ponents as props.

/** @jsx React.DOM */

var Inputs = React.createClass({

    render: function () {
        var items = [];
        var inputs;
        for (var i = 0; i < this.props.count; i++) {
            items.push( <input type="text" name={[i]} />);
            items.push(<br />);
        }
        return ( 
            <div className = "col-md-9"> 
                <form action = "/" method = "post" name = "form1"> 
                    {items} 
                    <input type="submit" className="btn btn-success" value = "Submit Form" />            
                </form>
            </div> 
       );
    }
});

var Add = React.createClass({
    render: function () {
        return (<input type = "button" className="btn" value="Add an Input" onClick={this.props.fnClick}/> );
  }
});

var Parent = React.createClass({
    getInitialState: function(){
        return {count:1}
    },
    addInput: function(){
        var newCount = this.state.count + 1;
        this.setState({count: newCount});
    },
    render: function(){
        return (
            <div>
                <Inputs count={this.state.count}></Inputs>
                <Add fnClick={this.addInput}/>
            </div>
        );
    }
});

React.renderComponent(<Parent></Parent> , document.body);

jsFiddle

You can call functions on the return value of renderComponent:

var Inputs = React.createClass({…});
var myInputs = React.renderComponent(Inputs);
myInputs.add();

The only way to get a handle to a React Component instance outside of React is by storing the return value of React.renderComponent. Source.

发布评论

评论列表(0)

  1. 暂无评论