javascript - react : updating parent state from nested children component

标签 javascript reactjs forms

我正在使用 React 来处理表单。我的想法是创建一个可重用的 Form 组件,它从 Page 组件获取状态作为 props,并保存使用子数据更新其自身状态的逻辑,将其发送到父 Page 组件。

页面组件是这样的:

class Page extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {
        text1: "Initial text1",
        text2: "Initial text2"
      }
    };
  }

  render() {
    return (
      <div className="Page">
        <div className="DataPreview">
          Data preview in Page component
          <div>{this.state.data.text1}</div>
          <div>{this.state.data.text2}</div>
        </div>
        <Form data={this.state.data}>
          <Input id="text1" data={this.state.data.text1} />
          <Input id="text2" data={this.state.data.text2} />
        </Form>
      </div>
    );
  }
}

这是表单组件:

class Form extends Component {
  constructor(props) {
    super(props);
    this.state = this.props.data;
  }

  render() {
    return (
      <div className="Parent">
        <div>Form component</div>
        <div className="DataPreview">
          Data preview in Form component
          <div>{this.state.text1}</div>
          <div>{this.state.text2}</div>
        </div>
        {this.props.children}
      </div>
    );
  }
}

这是输入组件:

class Input extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div className="Child" id={this.props.id}>
        <div>Input component</div>
        <input id={this.props.id} type="text" value={this.props.data} />
      </div>
    );
  }
}

所以输入应该更新表单状态,表单应该更新页面状态。我知道当输入写入表单组件内部时如何传递回调,但我不知道当输入写入页面组件内部时如何执行此操作,就像在本例中一样。

我有一个沙盒供感兴趣的人使用:https://codesandbox.io/s/qx6kqypo09

最佳答案

class Input extends Component {


constructor(props) {
    super(props);
  }

  handleChange(e) {
    let data = this.props.this.state.data;
    data.text1 = e.target.value;
    this.props.this.setState({ data: data });
  }

  render() {
    return (
      <div className="Child" id={this.props.id}>
        <div>Input component {this.props.id}</div>
        <input
          id={this.props.id}
          type="text"
          value={this.props.data}
          onChange={e => this.handleChange(e)}
        />
      </div>
    );
  }
}

使用指定的输入组件和如下所述的页面组件-

class Page extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {
        text1: "Initial text1",
        text2: "Initial text2"
      }
    };
  }

  render() {
    return (
      <div className="Page">
        <div className="DataPreview">
          Data preview in Page component
          <div>{this.state.data.text1}</div>
          <div>{this.state.data.text2}</div>
        </div>
        <Form data={this.state.data}>
          <Input id="text1" this={this} data={this.state.data.text1} />
          <Input id="text2" data={this.state.data.text2} />
        </Form>
      </div>
    );
  }
}

我想这会对你有帮助 谢谢

关于javascript - react : updating parent state from nested children component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52245531/

相关文章:

reactjs - 如何在 ReactJS 组件单元测试中获取 'mock' 状态值?

javascript - 如何使用 Bootstrap 使我的网站具有响应性?

javascript - window.opener跨域调用使用javascript

javascript - 从公共(public)文件夹 ReactJS 中获取本地 JSON 文件

delphi - 取消/中止在 Delphi/C++Builder 中创建新表单?

php - 在与表单相同的模式窗口中加载 php 成功消息

javascript - 我怎样才能创建这样的表格?

javascript - 按钮 onclick ="e.preventDefault(); return true;"给出 e 未在浏览器控制台中定义

javascript - Marionette - "startWithParent = false"会导致什么?

javascript - Prop (对象列表)更新后 react 不渲染