Answer the question
In order to leave comments, you need to log in
How to get state from another component in React.js?
In general, there are two components.
var Comp1 = React.createClass({
getInitialState: function() {
return {
data: []
};
},
componentWillMount: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'GET',
success: function(data) {
this.setState({
data: data
});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
render: function() {
return (
<div>
{this.state.data}
</div>
);
}
});
var Comp2 = React.createClass({
render: function() {
return (
<div>
{this.state.data} // ?
</div>
);
}
});
Answer the question
In order to leave comments, you need to log in
If you do not adhere to the flux architecture, then like this:
var Comp1 = React.createClass({
//code
render: function() {
return (
<div>
<Comp2 data={this.state.data} />
</div>
);
}
});
var Comp2 = React.createClass({
render: function() {
return (
var fromOne = this.props.data;
<div>
{fromOne}
</div>
);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question