2016-10-01 54 views
0

接收數據,現在我作出反應容器看起來是這樣的組件:我將如何構建反應,必須從一個模式

class TableData extends Component 
{ 
some React Functions and state changes. 
<Sidebar passdata as props/> 

} 

而且側邊欄我有一個模式,需要更新狀態裏面既邊欄組件和TableData組件。

我不知道你是否可以將道具傳遞給React中的父組件,但我知道不推薦。在這些類型的問題中推薦的行動方案是什麼?

+0

您是否在使用redux或類似的東西? – afuous

+0

目前不是,我應該嗎?我只有這兩個組件我有這個問題 –

+0

Redux對大型項目很有用,但對於小規模的項目並不需要。 – afuous

回答

2

你是正確的,你不能將道具傳遞給父組件。但是,父組件可以將回調作爲一個道具傳遞給它的子項,這些值在值更改時調用。

class TableData extends React.Component { 

    constructor(props) { 
     // ... 
     this.state = { 
      name: originalState, 
     }; 
    } 

    handleStateChange(newState) { 
     this.setState({ name: newState }); 
    } 

    render() { 
     // ... 
     <Sidebar onStateChange={this.handleStateChange.bind(this)} /> 
     // ... 
    } 
} 

class Sidebar extends React.Component { 
    // ... 
    this.props.onStateChange(newState); 
    // ... 
} 
相關問題