[英]Setting a changing prop value to a child state variable
Let's assume we have a two components (Parent and Child), basically I am sending my Parents state variable to my child's input element to do some manipulation. 假设我们有两个组件(“父级”和“子级”),基本上我是将“父级”状态变量发送到我的孩子的input元素上进行操作。 Something like...
就像是...
var Parent = React.createClass({
getInitialState: function() {
return {
name: ''
}
},
handleTextChange: function(e) {
this.setState({
name: e.target.value
})
},
render: function() {
return(
<div>
<input type="text" placeholder="Enter something" onChange={this.handleTextChange} />
<Child name={this.state.name} />
</div>
)
}
});
And my child component looks like this.. 我的子组件看起来像这样。
var Child = React.createClass({
getInitialState: function() {
return {
childName: ''
}
},
componentWillReceiveProps: function(nextProps) {
this.setState({
childName: nextProps.name
})
},
handleChildTextChange: function(e) {
this.setState({
childName: e.target.value
})
},
render: function() {
return(
<div>
<input type="text" onChange={this.handleChildTextChange} placeholder={this.props.name} />
<h4>{this.state.childName}</h4>
</div>
)
}
});
Basically I want to do 3 things 基本上我想做三件事
name
) name
) Child
's input, the target text should override my Parent's name(this.props.name) Child
的输入后,目标文本应覆盖我父母的名字(this.props.name) Parent
's name is changing, I want my Child
's component state variable childName
to be that of the changing prop coming from the parent(this.props.name) Parent
的名字正在改变,我希望我的Child
的组件状态变量childName
是来自父母的改变道具的名字(this.props.name) Can anybody please help me with this? 有人可以帮我吗?
PS : My Child
's components target text shouldn't update my Parent
's state variable(name) I mean, no Callbacks as props. PS:我
Child
的组件目标文本不应更新我Parent
的状态变量(名称),我的意思是,没有回调作为道具。
I think you should only set the state to your this.props.name when getting the initial state value, and not on each componentWillReceiveProps sample. 我认为您应该仅在获取初始状态值时将状态设置为this.props.name,而不是在每个componentWillReceiveProps示例上。 Once the child sets it's own state, the value will only change in the child and not in the parent
一旦子级设置了自己的状态,则该值将仅在子级中更改,而不在父级中更改
var Child = React.createClass({ getInitialState: function() { return { childName: this.props.name, owned: false } }, componentWillReceiveProps: function(nextProps) { // only do it if the state isn't owned by the child if (!this.state.owned) { this.setState({ childName: nextProps.name, owned: false }); } }, handleChildTextChange: function(e) { this.setState({ childName: e.target.value, owned: true }); }, render: function() { return( <div> <input type="text" onChange={this.handleChildTextChange} placeholder={this.props.name} /> <h4>{this.state.childName}</h4> </div> ) } }); var Parent = React.createClass({ getInitialState: function() { return { name: '' } }, handleTextChange: function(e) { this.setState({ name: e.target.value }) }, render: function() { return( <div> <input type="text" placeholder="Enter something" onChange={this.handleTextChange} /> <Child name={this.state.name} /> </div> ) } }); ReactDOM.render(<Parent />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.