reactjs检索所有子引用父事件(handleSubmit)

时间:2017-05-30 04:40:09

标签: reactjs events children refs

<Parent> /* event to fetch all refs value and submit */
  <Child>
     <stateless function />/*this is all the refs sittin*/
  </Child>
</Parent>

我不知道父母如何从孩子身上获得所有裁判?提前致谢

1 个答案:

答案 0 :(得分:1)

我知道这可能为时已晚,你已经知道了答案。 你需要的是一个&#34; ref&#34;,一个对孩子的参考。 阅读: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>
    )
  }
}