通过道具传递价值从无国籍的孩子到父母 [英] Passing Values via Props From React Stateless Child to Parent

查看:106
本文介绍了通过道具传递价值从无国籍的孩子到父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CONTEXT

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

I'm trying to pass input value fields (conditionTitle) from a React Stateless child component (AddConditionSelect) to the parent component (AddConditionDashboard) that will hold my state.

PROBLEM

我遵循 React documentation ,但是它们使用的引用仅在组件有状态时才有效。我不想在子组件中设置任何状态,但仍然可以访问父级中的输入。

I followed the model shown in the React documentation, but they are using refs, which only works if the component is stateful. I do not want to have to set any state in the child component, but still be able to access the input in the parent.

在其当前形式中,我收到一条警告,无状态函数组件不能被赋予参考,导致道具为空且未定义。

In its current form, I am getting a Warning, that the stateless function components cannot be given refs, resulting in props being null and 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;

子组件:

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;


推荐答案

如何将事件处理程序直接传递给<输入> ?这样你就可以将更改事件直接传递给您的父级(< Input> 的祖父母),您可以从 event.target中提取值。值所以不需要使用参考:

How about passing the event handler directly to <Input>? This way you pass the on change event directly to your parent (grandparent of <Input>) and you can extract the value from event.target.value so no need to use refs:

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

Note: You might have to bind the context of onUserInputChange() in you parent's constructor because event handlers have the element on which the event happened as their context by default:

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>
    />

    // ...
  }
  // ...

孩子:

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>
    );
  }
}

这篇关于通过道具传递价值从无国籍的孩子到父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆