通过道具将值从React Stateless Child传递给Parent

Dan*_*Dan 5 javascript ecmascript-6 reactjs

CONTEXT

我正在尝试将输入值字段(conditionTitle)从React无状态子组件(AddConditionSelect)传递到AddConditionDashboard将保持我的状态的父组件().

问题

我遵循React文档中显示的模型,但它们使用的是refs,只有在组件是有状态的情况下才有效.我不想在子组件中设置任何状态,但仍然可以访问父组件中的输入.

在其当前形式中,我得到一个警告,无状态函数组件不能被赋予refs,导致props为null和undefined.

父组件:

import AddConditionSelect from '../containers/AddConditionSelect.js';

class AddConditionDashboard extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      conditionTitle: '',
      conditionType: ''
    };
  }

  handleUserInput({conditionTitleInput}) {
    this.setState({
      conditionTitle:conditionTitle
    })

  }

  render() {
    const {error, segmentId} = this.props;

    return (
      <div>

    <AddConditionSelect segmentId={segmentId} conditionTitle={this.state.conditionTitle} onUserInput={this.handleUserInput} />


    <PanelFooter theme="default">
      <Button backgroundColor="primary" color="white" inverted={true} rounded={true} onClick={(event) => this.onSubmit(event)}>
        Next Step
      </Button>
    </PanelFooter>

      </div>
    );
  }

}

export default AddConditionDashboard;
Run Code Online (Sandbox Code Playgroud)

子组件:

class AddConditionSelect extends React.Component {

  onInputChange: function() {
    this.props.onUserInput(
      this.refs.conditionTitleInput.value,
    )
  },

  render() {
    const {error} = this.props;

    return (
      <div>

        <Panel theme="info">

        <Divider />

        Please enter a name {error ? <Message inverted={true}  rounded={true}  theme="error">{error}</Message>  : null}
          <Input value={this.props.conditionTitle} ref="conditionTitleInput" label="" type="text" buttonLabel="Add Condition" name="add_segment" onChange={this.onInputChange} placeholder="Condition Title"/>

       </Panel>
     </div>
    );
  }

}
export default AddConditionSelect;
Run Code Online (Sandbox Code Playgroud)

nem*_*035 6

如何将事件处理程序直接传递给<Input>?这样你就可以将on change事件直接传递给你的父(祖父母<Input>),你可以从中提取值,event.target.value所以不需要使用refs:

注意:您可能必须使用父级构造函数中bind的上下文,onUserInputChange()因为事件处理程序默认情况下将事件发生的元素作为其上下文:

class AddConditionDashboard extends React.Component {

  constructor(props) {
    // ...

    // bind the context for the user input event handler
    // so we can use `this` to reference `AddConditionDashboard`
    this.onUserInputChange = this.onUserInputChange.bind(this);
  }

  onUserInputChange({ target }) {
    const { value: conditionTitle } = target;
    this.setState({
     conditionTitle
    });
  }

  render() {
    // ...

    <AddConditionSelect segmentId={segmentId} 
                        conditionTitle={this.state.conditionTitle} 
                        onUserInputChange={this.onUserInputChange} // <-- pass event handler to child that will pass it on to <Input>
    />

    // ...
  }
  // ...
Run Code Online (Sandbox Code Playgroud)

儿童:

class AddConditionSelect extends React.Component {

  render() {
    const { error } = this.props;

    return (
      <div>
        // ...

        <Input value={this.props.conditionTitle} 
               label="" 
               type="text" 
               buttonLabel="Add Condition" 
               name="add_segment" 
               onChange={this.props.onUserInputChange} // <-- Use the grandparent event handler
               placeholder="Condition Title"
        />

       // ...
     </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)