reactjs检索所有儿童参考父母事件(handleSubmit)
问题描述:
<Parent> /* event to fetch all refs value and submit */
<Child>
<stateless function />/*this is all the refs sittin*/
</Child>
</Parent>
我不知道父母如何从孩子获得所有参考?在此先感谢reactjs检索所有儿童参考父母事件(handleSubmit)
答
我知道这可能太迟了,你已经知道答案。 你需要的是一个“参考”,对孩子的参考。 阅读: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>
)
}
}
你能与例如输入的代码添加和预期比你的结构outputrather。 –