简体   繁体   English

将更改的prop值设置为子状态变量

[英]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 基本上我想做三件事

  1. My Child component's childName value should be set initially to that of parent's prop(this.props.name which is Parent's state variable name ) 我的Child组件的childName值应最初设置为父项的prop(此值是props.name,即父项的状态变量name
  2. On change of my Child 's input, the target text should override my Parent's name(this.props.name) 更改我Child的输入后,目标文本应覆盖我父母的名字(this.props.name)
  3. If my child input is empty and my 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.

相关问题 道具值在孩子身上没有变化 - Prop value not changing in child 反应:无法访问状态变量的值,并且数组或迭代器中的每个子代都应具有唯一的“键”属性 - React: Not able to access the value of state variable and each child in an array or iterator should have a unique “key” prop Stencil JS - 使用 Prop 值启动状态变量? - Stencil JS - Initiate a State variable with a Prop value? 为什么我的 state 变量没有作为道具传递给子元素? - Why is my state variable not getting passed as prop to a child element? 在反应中传递状态或道具以将值传递给子组件 - Passing a state or prop to pass a value to child component in react 基于另一个状态道具的 Onchange 更改组件状态道具 - Changing a component state prop based on a Onchange of another state prop 根据子组件中的变量 state 在 NuxtJS 中设置标题 - setting title in NuxtJS depeding on variable state in child component 将变量的值设置为另一个变量的先前状态值 - Setting the value of a variable as previous state value of another variable 在子组件中设置 prop 的值并将值传回 app.js 的状态,做出反应 - Set value of prop in child component and pass value back to state of app.js, react React Redux state 数组变量作为道具传递给子组件,无限循环或空数组 - React Redux state array variable pass as prop to child component, either infinite loop or empty array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM