2015-09-06 12 views
1

所以,在反應過來,我們知道我們可以有孩子通過這樣的回調與家長溝通:如何通過祖父母組件中的「子女」動態定義父母/子女組成部分之間的通信?

Child = React.createClass({ 
    render() { 
    return (
     <div> 
     <button onClick={this.props.onAction.bind(null, this.props.name)}>Child button</button> 
     </div> 
    ) 
    } 
}); 

Parent = React.createClass({ 
    handleChildAction(name) { 
    alert(`Child button ${name} clicked`); 
    }, 

    render() { 
    return (
     <div> 
     <Child name="robot1" onAction={this.handleChildAction} /> 
     </div> 
    ) 
    } 
}); 

但我不清楚如何可以有這個父母/子女之間的雙向通信一種格式:

Container = React.createClass({ 
    render() { 
    return (
     <Parent> 
     <Child name="robot1" /> 
     <Child name="robot2" /> 
     </Parent> 
    ) 
    } 
}); 

我怎樣才能得到Parent向下發送信息給孩子,並有Child發送信息嗎?

+1

http://facebook.github.io/react/docs/two-way-binding-helpers.html –

+1

不錯!這是通常回調結構的一個很好的捷徑,但它並不能解釋如何在上面的場景#2中讓'Parent'與孩子交談。 – ffxsam

回答

1

這是一個情況下,我一般儘量避免,但是當父母表示特定類型的孩子的一些容器,或者應該提出一些其他類型的抽象的也可以是有用in certain situations

當你覺得有必要,你可以利用mapping over this.props.children裏面Parentalong with React.cloneElement給他們傳遞額外的道具。請記住,您應該將this.props.children視爲不透明,並且只能通過React.Children幫助程序進行迭代。例如:

var Child = React.createClass({ 
    render() { 
    return (
     <div> 
     <button onClick={this.handleClick}>Child button</button> 
     </div> 
    ) 
    }, 

    handleClick(e) { 
    this.props.onAction(this.props.name); 
    } 
}); 

var Parent = React.createClass({ 
    handleChildAction(name) { 
    alert(`Child button ${name} clicked`); 
    }, 

    render() { 
    return (
     <div>{React.Children.map(this.props.children, this.renderChild)}</div> 
    ) 
    }, 

    renderChild(child) { 
    return React.cloneElement(child, {onAction: this.handleChildAction}); 
    } 
}); 

var Container = React.createClass({ 
    render() { 
    return (
     <Parent> 
     <Child name="robot1" /> 
     <Child name="robot2" /> 
     </Parent> 
    ); 
    } 
}); 

這裏的a working JSBin演示了該技術:https://jsbin.com/mijune/edit?js,output

+0

我注意到'Container'組件中,你傳遞給'Child'的是下面的prop:'onAction = {this.handleChildAction}',但'Container'中沒有'handleChildAction'方法。或者'this'是指'Parent'? – ffxsam

+0

我現在明白了。看起來你甚至不需要'Container'組件中的'onAction = {this.handleChildAction}'。我刪除了這些,並且它仍然正常工作,因爲您在克隆子項時添加處理程序道具。 – ffxsam

+0

@ffxsam啊,是的,這是一個錯字。我會解決的! –

0

你沒有提到祖父母。這個怎麼樣。

Child = React.createClass({ 
    handleAction(name) { 
     this.props.onAction(this.props.name); 
    }, 
    render() { 
     return (
      <div> 
       <button onClick={this.handleAction}>Child button</button> 
      </div> 
     ) 
    } 
}); 

Parent = React.createClass({ 
    getInitialState: function() { return {data: ''}; }, 
    handleChildAction(name) { 
     alert(`Child button ${name} clicked`); 
     this.props.grandParentAction(data); 
    }, 

    render() { 
     return (
      <div> 
       <Child name="robot1" onAction={this.handleChildAction} parentData={this.props.grandParentData} /> 
      </div> 
     ) 
    } 
}); 

GrandParent = React.createClass({ 
    getInitialState: function() { return {data: ''}; }, 
    handleParentAction(name) { 
     alert(`Parent passed ${name}`); 
     this.setState({data: 'newData'}); 
    }, 

    render() { 
     return (
      <div> 
       <Child name="robot1" onAction={this.handleChildAction} parentData={this.state.data} /> 
      </div> 
     ) 
    } 
});  
+0

它的確如此,但'Parent'不能包含特定的硬編碼子代。 「父」包含在一個更大的組件中,該組件將定義孩子是什麼。 – ffxsam

+0

然後,父母需要將狀態傳遞給其父母,並將它的道具而不是其狀態發送給孩子。 –

+0

或將祖父母處理程序傳遞給孩子。 –