I am trying to get a button passed by another component to refer and / or influence the state of another component.
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 component that can access the add function in Inputs. I tried to reference it directly using Inputs.add as follows:
var Add = React.createClass({ render: function(){ return ( <input type="button" className="btn" value="Add an Input" onClick={Inputs.add} /> ); } });
But that did not work. How can I access components through another component or affect the state of a component through another component? Thanks.
javascript reactjs
user2829448
source share