所以我得到了App
,它實現了componentDidMount
和render
。是否有可能從React中的Container中觸發Contained Component的渲染?
應用程序包含2個組件,一個是AutoComplete
輸入,另一個是CardView
。
的計劃是,一旦用戶已經從AutoComplete
列表中選擇一個項目,在CardView
我不知道它顯示我怎麼會引發CardView
的渲染(即設置它的狀態)從AutoComplete
內事件處理器。
見讀取行:
//願意觸發CardView的呈現在這裏。
在下面的代碼中。
import React, { Component } from 'react';
import injectTapEventPlugin from 'react-tap-event-plugin';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import AutoComplete from 'material-ui/AutoComplete';
// Needed for onTouchTap
// http://stackoverflow.com/a/34015469/988941
injectTapEventPlugin();
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {accounts: []}
this.showAccount = this.showAccount.bind(this);
}
componentDidMount() {
this.setState({ accounts: [
{account_name: "foo", account_id: 1},
{account_name: "bar", account_id: 2}
]})
}
showAccount (value) {
// willing to trigger CardView's render right here.
}
render() {
return (
<MuiThemeProvider>
<div className="App">
<center>
<AutoComplete
floatingLabelText="account name"
filter={AutoComplete.caseInsensitiveFilter}
dataSource={this.state.accounts.map((account) => account.account_name)}
onUpdateInput={this.showAccount}
/></center>
<CardView />
</div>
</MuiThemeProvider>
);
}
}
export default App;
請查閱https://facebook.github.io/react/docs/refs-and-the-dom.html。 –
爲什麼不通過道具卡?這樣,您可以在您的show account功能中設置狀態並將該狀態傳遞給卡。 –
@usedToBeFat在聲明時,選擇的值是已知的。如何從'showAccount'處理程序中引用Card的道具? –