简体   繁体   English

将道具/状态传递回React JS中的父组件

[英]Passing props / state back to parent component in React JS

I have two components, a select list with several options and a button component 我有两个组件,一个带有多个选项的选择列表和一个按钮组件

What I'd like to do in the UI is that the user can select any option from the select list they want and then press the button to reset the list to 'select' 我要在用户界面中执行的操作是用户可以从所需的选择列表中选择任何选项,然后按按钮将列表重置为“选择”

I'm keeping the parent component as the one source of truth - all updated states are passed back to the parent component, I've actually got this working great in the context of one file with the following code; 我将父组件作为事实的唯一来源-所有更新后的状态都传递回父组件,实际上,在具有以下代码的一个文件的上下文中,这种工作非常有效;

class SelectList extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.props.onSelectListChange(e.target.value);
  }

  render() {
    const selectedValue = this.props.selectedValue;
    log.debug('SelectListValue(): ', this.props.selectedValue);

    return (
      <select value={selectedValue} onChange={this.handleChange}>
        <option value='One'>One</option>
        <option value='select'>select</option>
        <option value='Three'>Three</option>
        <option value='Four'>Four</option>
        <option value='Five'>Five</option>
      </select>
    );
  }
}

class SelectListReset extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.props.onResetChange('select');
  }

  render() {
    return (
      <div>
        <button onClick={this.handleChange}>Reset list to select</button>
      </div>
    );
  }
}

class Page extends Component {
  constructor(props) {
    super(props);
    this.state={
      selectedValue: 'select'
    }
    this.handleSelectedListChange = this.handleSelectedListChange.bind(this);
    this.handleResetChange = this.handleResetChange.bind(this);
  }

  handleSelectedListChange(selectedValue) {
    this.setState({selectedValue});
  }

  handleResetChange() {
    this.setState({selectedValue: 'select'});
  }

  render() {
    log.debug('render(): ', this.props);
    log.debug('ParentListValue(): ', this.state.selectedValue);
    return (
    <div className="content-area">
        <div className="container">
            <h1>{LANGUAGES_CONTROLLER.format(this.props.languages, 'TitleSettings')}</h1>

            <div>
              <SelectList
                onSelectListChange={this.handleSelectedListChange}
                selectedValue={this.state.selectedValue}
              />

              <SelectListReset 
                onResetChange={this.handleResetChange}
                selectedValue={this.state.selectedValue}
              />

            </div>

        </div>
    </div>
    );
}

But what I've actually like to do is move the reset button to it's own file and this is where I fall over trying to pass the props / state back to the parent. 但是我实际上想做的是将“重置”按钮移到它自己的文件中,这就是我试图将props / state传递回父对象的地方。

So the render method would actually look like this 所以render方法实际上看起来像这样

render() {
  log.debug('render(): ', this.props);
  log.debug('ParentListValue(): ', this.state.selectedValue);
  return (
    <div className="content-area">
      <div className="container">
        <h1>{LANGUAGES_CONTROLLER.format(this.props.languages, 'TitleSettings')}</h1>

        <div>
          <SelectList
            onSelectListChange={this.handleSelectedListChange}
            selectedValue={this.state.selectedValue}
          />

          <TestComponent 
            onResetChange={this.handleResetChange}
            selectedValue={this.state.selectedValue}
          />

        </div>

      </div>
    </div>
  );
}

I import TestComponent and then inside of TestComponent is where I will have my code for the SelectListReset component but the problem I'm having is that now the values are sent to it as props which should be immutable right so can't be updated? 我导入了TestComponent,然后在TestComponent的内部放置了SelectListReset组件的代码,但是我遇到的问题是,现在这些值作为道具发送给它了,应该是不可变的,因此无法更新?

That's where my understand stops .. if someone can point me in the right direction on this I'd be very grateful! 那就是我的理解停止的地方..如果有人可以指出我正确的方向,我将非常感激!

Props received from a parent will change if the relevant state in the parent is changed. 如果更改了父级中的相关状态,则从父级收到的道具也会更改。 There's no problem with this and all the re-rendering is handled by React. 这没有问题,所有重新渲染都由React处理。 You can pass props down as many levels as you like. 您可以根据需要将道具传递至任意多个级别。

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

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