我有1個父組件名稱[Parent-1]和1個子組件名稱[Child-1]。現在,我還有其他幾個組件名稱[Other-1]和[Other-2]。兒童組件通訊
現在我將[Other-1]和[Other-2]組件傳遞給[Child-1]組件。 JSX渲染正確。我如何從[Child-1]訪問[Other-1/2]組件功能?或者我如何將道具從[Other-1/2]傳遞給[Child-1]?
通過使用引用我可以從[Parent-1]調用[Other-1/2]函數,但我想從[Child-1]訪問。我嘗試將參數傳遞給[Child-1],如<Child abc={() => this.refs.other1.hello()}/>
或<Child abc={this.refs.other1.hello()}/>
,但這不起作用。
通過使用全局事件發射器的方式,我能夠實現解決我的上述問題。但不知道這是否是React.js中的適當方式
此外還得到幫助從http://andrewhfarmer.com/component-communication/ –
雖然還有1個問題ReactDOM.findDOMNode(this.refs.something)在放入log()函數並從Child –
調用log()函數時總是返回null'this.refs.something'被稱爲字符串refs和那些已被棄用。 –