ReactJS-在componentDidMount中分派并在render中使用Redux属性

ReactJS-在componentDidMount中分派并在render中使用Redux属性,reactjs,redux,react-redux,Reactjs,Redux,React Redux,你好,伙计们!» 在componentDidMount中,我发送了一个更改redux属性id的操作,但在渲染时,redux状态中的属性id为null 有没有办法解决这个问题 componentDidMount() { const{match:{params:{roomId}}, getItem, getDirectInfo, getIndirectInfo, getType} = this.props; if(roomId !== undefined) { const

你好,伙计们!»

在componentDidMount中,我发送了一个更改redux属性id的操作,但在渲染时,redux状态中的属性id为null

有没有办法解决这个问题

componentDidMount() {
    const{match:{params:{roomId}}, getItem, getDirectInfo, getIndirectInfo, getType} = this.props;
    if(roomId !== undefined) {
      const type = getType(roomId);
      if(type.data === 'DIRECT') getDirectInfo(roomId).then(roomId => getInfo(roomId));
      else if(type.data === 'INDIRECT') getIndirectInfo(roomId).then(roomId => getInfo(roomId));
    }
 }

getInfo更改prop id的值,但在渲染时它为null,这可能是null,因为您在reducer中将它初始化为null,并且在调用render时还没有调用调度。

我假设问题是一个错误,因为您尝试使用id,但它未定义

在react生命周期中,在componentDidMount之前调用render函数。这意味着您应该能够处理呈现时未定义的id,我建议显示某种加载指示器


由于您没有展示一个完整的示例来再现这一点,因此可能还存在其他问题,请确保减速器实际工作并且您的部件连接正确。

编写一些代码。否则,您不可能提供您所面临问题的示例。没有示例,建议仅限于对代码进行猜测。我认为这非常简单。。。在componentDidMount中,我调用getSomething来更改redux中的prop id,redux在被调度后用null初始化,它应该被重新调用,其中它不应该为null