简体   繁体   English

当子组件作为属性传递时,react状态不会更新/传递给子组件

[英]react state is not updated/passed to child component when child component is passed as a property

I am trying to create dynamic form, so I pass some jsx elements to a child component as a property. 我正在尝试创建动态表单,因此我将一些jsx元素作为属性传递给子组件。 Even though the state is being updated, the updated state is not passed to the element. 即使状态正在更新,更新后的状态也不会传递给元素。 Below is the code: 下面是代码:

This is the child component which maps over the passed controls and outputs them. 这是子组件,用于映射传递的控件并输出它们。

class Form extends Component {
  render() {
    return (
      <div>
        {this.props.controls.map((c, i) => {
          return <React.Fragment key={i}>{c}</React.Fragment>;
        })}
      </div>
    );
  }
}

This is the App that calls the child component: 这是调用子组件的应用程序:

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      username: '',
      password: ''
    };

    this.controls = [
      <input
        type="text"
        onChange={this.onChangeUsername}
        value={this.state.username}
      />
    ];
  }

  componentDidUpdate() {
    console.log(this.state);
  }

  render() {
    return (
      <div className="App">
        <div className="app__group">
          <h1>This is not working</h1>
          <Form controls={this.controls} />
        </div>
        <div className="app__group">
          <h1>This is working</h1>
          <input
            type="text"
            onChange={this.onChangePassword}
            value={this.state.password}
          />
        </div>
      </div>
    );
  }

  onChangeUsername = e => {
    console.log('onChangeUsername', e.target.value);
    this.setState({ username: e.target.value });
  };

  onChangePassword = e => {
    console.log('onChangePassword');
    this.setState({ password: e.target.value });
  };
}

As an example of the unexpected behaviour, when an input passed as a property to the child component, I cannot type in the input. 作为意外行为的示例,当输入作为属性传递给子组件时,我无法键入输入。 The state gets updated but it's is not passed to the child, thus the text does not show. 状态会更新,但不会传递给孩子,因此不会显示文本。

On the other hand, a standard input element works, I can type and see the output. 另一方面,可以使用标准输入元素,我可以键入并查看输出。

What am I doing wrong? 我究竟做错了什么?

the problem is that you are trying to make something "fixed" as something dynamic. 问题是您试图使某些东西“固定”为动态。 lets go with a more functional approach and it will refresh each one of the inputs like if they are dynamic. 让我们采用一种更实用的方法,它将刷新每个输入,例如它们是动态的。

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      username: '',
      password: ''
    };
  }

  componentDidUpdate() {
    console.log(this.state);
  }

  render() {
    return (
      <div className="App">
        <div className="app__group">
          <h1>This is not working</h1>
          <Form controls={this.controls()} />
        </div>
      </div>
    );
  }

  controls = () => {
    return [<input
            type="text"
            onChange={this.onChangeUsername}
            value={this.state.username}
           />]
  }

  onChangeUsername = e => {
    console.log('onChangeUsername', e.target.value);
    this.setState({ username: e.target.value });
  };

  onChangePassword = e => {
    console.log('onChangePassword');
    this.setState({ password: e.target.value });
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM