【发布时间】:2017-04-08 00:09:15
【问题描述】:
所以我得到了App,它实现了componentDidMount 和render。
App 包含 2 个组件,一个是 AutoComplete 输入,另一个是 CardView。
计划是一旦用户从AutoComplete 列表中选择了一个项目,将其显示在CardView 中
我想知道如何从AutoComplete 事件处理程序中触发CardView 的渲染(即设置它的状态)。
请参阅以下内容:
//愿意在这里触发 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;
【问题讨论】:
-
为什么不将道具传递给卡片?这样您就可以在您的节目帐户函数中设置状态并将该状态传递给卡片。
-
@usedToBeFat 在声明时,选择的值是已知的。如何在
showAccount处理程序中引用 Card 的道具? -
在您的问题中,您声明一旦用户从自动完成列表中选择了一个项目,您就想显示该项目。如果我理解正确,那么您不需要卡片的渲染方法。你只需要它调用 setState 这将导致另一个渲染,这一次卡片将获得它将显示的道具。还是我错过了什么?
-
@usedToBeFat - 如果您可以使用我的代码来解释您的意思,那将是最好的。
标签: javascript reactjs