2017-05-30 64 views

回答

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> 
    ) 
    } 
} 
+0

请提供有关解决方案的更多详细信息,可能使用代码示例,而不是仅支持答案的链接。 – Fabien

+0

请检查我的修改答案。 – Win

+0

Upvoted的努力。 – Fabien