【问题标题】:ReactJS - Child props are not updating when child is rendered through a variableReactJS - 通过变量呈现子时,子道具不会更新
【发布时间】: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


【解决方案1】:

您对 state 变量渲染的组件的 props 不会更新,因为该变量不会在每次渲染时更新,而是仅在您调用 setState 时更新,

您应该改为有条件地呈现页面或使用路由器。 您也可以简单地使用处理程序并将需要它的页面作为参数传递给它

使用条件渲染,您的代码将如下所示

class ManagerTeam extends Component {

  constructor(props) {
    super(props)
    this.initComponent = this.initComponent.bind(this)
    this.showPage = this.showPage.bind(this)
    this.initComponent()
  }

  initComponent() {
    this.updateData(this.props)
    this.state = {
      currentpage: <ManagerTeamVisaoGeral teamList={this.teamList}/>
    }
  }

  updateData(props) {
    this.teamList = props.userTeams.list
  }

  showPage(page) {
    this.setState({
      currentpage: page
    })
  }


  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 === 'Visão Geral' && <ManagerTeamVisaoGeral teamList={this.teamList}/>}
        {this.state.currentpage === 'Membros' && <ManagerTeamMembros/>}
        {this.state.currentpage === 'Tarefas' && <ManagerTeamTarefas/>}
        {this.state.currentpage === 'Report' && <ManagerTeamReport/>}

      </div>
    )
  }
}

【讨论】:

  • 我理解这种方法,它将解决我的问题。但是,我正在寻找一种方法来更新子道具,而不必在每次渲染时重新分配 currentpage
【解决方案2】:

这是因为即使您正在更新列表,但您的组件没有再次呈现。因此,最好将您的teamList 设置为state variable 并在您更新列表时执行this.setState,还有一件事是尝试在您的componentWillReceiveProps 中设置一个if 条件,如下所示:

componentWillReceiveProps(nextProps) {
 if(this.state.teamList !== nextProps.userTeams.list){
  // update will be called only when you have new list
  this.updateData(nextProps)
  }
 }

【讨论】:

  • 这是我的第一个想法。但是我不能同时将 teamListcurrentpage 作为状态变量。所以我发起了 this.currentpage = 问题仍然存在。 teamList 得到更新,但 currentpage 没有。我将始终必须强制更新 currentpage
  • 在这种情况下,使用render 方法中的条件渲染来更改您的“当前页面”,我猜这会解决问题
  • 是的,它会解决问题的。我只想知道一种无需条件渲染即可解决此问题的方法,因为如果需要,更新和重构会变得更加困难。
猜你喜欢
  • 2019-01-28
  • 2020-06-27
  • 2020-03-26
  • 1970-01-01
  • 2016-11-03
  • 2018-08-09
  • 2020-09-24
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多