我刚开始学习React,在我的
我希望只有在从
ParentComponent
中,我通过输入框获得了一个值,现在想将这个值传递给我的ChildComponent
,并使用它来运行AJAX调用。我尝试过通过替换ParentComponent
中的状态来实现,但是我仍然无法在ChildComponent
中获取它。我希望只有在从
ParentComponent
接收到输入值后才渲染/运行ChildComponent
(以便我可以运行AJAX调用,然后再进行渲染...)。有什么提示吗?var ParentComponent = React.createClass({
getInitialState: function() {
return {data: []};
},
handleSearch: function(event) {
event.preventDefault();
var userInput = $('#userInput').val();
this.replaceState({data: userInput});
},
render: function() {
return (
<div>
<form>
<input type="text" id="userInput"></input>
<button onClick={this.handleSearch}>Search</button>
</form>
<ChildComponent />
{this.props.children}
</div>
);
}
});
var ChildComponent = React.createClass({
render: function() {
return <div> User's Input: {this.state.data} </div>
}
});