【发布时间】:2018-10-21 20:04:32
【问题描述】:
我的目标是通过this.state.currentpage渲染不同的组件。一切都按预期工作,除非我需要更新渲染组件的道具。这是一个问题,因为 teamList 一直在变化。我已使用当前更改更新了 teamList,但我无法接收存储在 this.state.currentpage 中的组件中的更改。
class ManagerTeam extends Component {
constructor(props) {
super(props)
this.initComponent = this.initComponent.bind(this)
this.showTeamMembros = this.showTeamMembros.bind(this)
this.showTeamReport = this.showTeamReport.bind(this)
this.showTeamTarefas = this.showTeamTarefas.bind(this)
this.showVisaoGeral = this.showVisaoGeral.bind(this)
this.updateData = this.updateData.bind(this)
this.initComponent()
}
initComponent() {
this.updateData(this.props)
this.state = {
currentpage: <ManagerTeamVisaoGeral teamList={this.teamList}/>
}
}
updateData(props) {
this.teamList = props.userTeams.list
}
showTeamMembros() {
this.setState({
currentpage: <ManagerTeamMembros/>
})
}
showTeamReport() {
this.setState({
currentpage: <ManagerTeamReport/>
})
}
showTeamTarefas() {
this.setState({
currentpage: <ManagerTeamTarefas/>
})
}
showVisaoGeral() {
this.setState({
currentpage: <ManagerTeamVisaoGeral teamList={this.teamList}/>
})
}
componentWillReceiveProps(nextProps) {
this.updateData(nextProps)
}
render() {
return (
<div>
<SimpleNavBar user={this.props.user} updateArtifacts={this.props.updateArtifacts} updateManagerTeams={this.props.updateManagerTeams}/>
<NavBar navBarTitle='Equipas' menuOptions={['Visão Geral', 'Membros', 'Tarefas', 'Report']} loadOptions={[this.showVisaoGeral, this.showTeamMembros, this.showTeamTarefas, this.showTeamReport]}/>
<Sidebar activePage="team" isManager={true}/>
{this.state.currentpage}
</div>
)
}
}
【问题讨论】:
-
在哪里调用函数来改变
state? -
我已经编辑了代码并添加了组件的其余部分。
标签: reactjs components react-props