【问题标题】:reactjs retrieve all children refs parent event (handleSubmit)reactjs 检索所有子 refs 父事件(handleSubmit)
【发布时间】:2017-05-30 04:40:09
【问题描述】:
<Parent> /* event to fetch all refs value and submit */
  <Child>
     <stateless function />/*this is all the refs sittin*/
  </Child>
</Parent>

我不知道父母如何从孩子那里获得所有参考?提前致谢

【问题讨论】:

  • 你可以添加带有示例输入和预期输出的代码,而不是你的结构。

标签: reactjs events children refs


【解决方案1】:

我知道可能为时已晚,而您已经知道答案了。 您需要的是“参考”,即对孩子的参考。 阅读:Exposing DOM Refs to Parent Components

类似这样的:

class Parent extends React.Component {
  render() {
    return (
      <div>
        // this line will create a reference to the input text,
        // inside the Child Component
        // for example, if you want to get the value 
        // you can access `this.input.value`
        <Child inputRef={(refToChild) => this.input = refToChild} />
      </div>
    )
  }
}

class Child extends React Component {
  render() {
    return (
      <form>
        // this line will call the Parent's function,
        // creating a reference to the input text.
        <input type="text" ref={props.inputRef} />
      </form>
    )
  }
}

【讨论】:

  • 请提供有关解决方案的更多详细信息,可能提供代码示例而不是应该只支持答案的链接。
  • 请检查我修改后的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
相关资源
最近更新 更多