2017-05-30 62 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