【问题标题】:cannot update during an existing state transition react native在现有状态转换期间无法更新本机反应
【发布时间】:2019-08-29 11:42:02
【问题描述】:

在我的应用程序中,我可以使用以下函数从数组中获取位置的值、距离和名称。但是,我无法使用 mapDispatchToProps 在我的 redux 存储中调度从它们获得的值, 例如

      handleNavigation() {
        this.props.navigation.navigate('LocationLists');
        this.props.totalDistanceChange(this.totalDistance()).
      }


      <Button
        onPress={this.handleNavigation.bind(this)}
      />





    const mapDispatchToProps = (dispatch) => ({
     totalDistanceChange: totalDistance=> {
       dispatch(totalDistanceChange(totalDistance));
     }
    });

在现有状态转换期间,我一直无法更新。 以下只是我的功能,因为我想让它尽可能简单,请在适当的地方更正。

      totalDistance = () => {
        const { selectedItemObjects } = this.state;
        const total = selectedItemObjects.reduce((result, { Distance }) => result += Distance, 0);
        return total.toFixed(1);
      }

      totalValue = () => {
        const { selectedItemObjects } = this.state;
        const total = selectedItemObjects.reduce((result, { Value }) => result += Value, 0);
        return total;
      }

      renderLocationText = () => {
        const { selectedItemObjects } = this.state;
        return selectedItemObjects.length ?
        `${selectedItemObjects.map((item, i) => {
          let label = `${item.name}, `;
          if (i === selectedItemObjects.length - 2) label = `${item.name} and `;
          if (i === selectedItemObjects.length - 1) label = `${item.name}.`;
          return label;
        }).join('')}`
        :
        null;
      }

我的问题是如何将获得的值传递给我的 redux 存储

【问题讨论】:

  • 请插入您呼叫this.props.totalDistanceChange(this.totalDistance())的上下文。通常你提到的错误与this.setState()上的一些无限循环有关。
  • 对上面的代码做了必要的修改

标签: reactjs react-native redux react-redux


【解决方案1】:

通过将其转换为字符串来修复它

  handleNavigation() {
    this.props.navigation.navigate('LocationLists');
    this.props.totalDistanceChange(this.totalDistance().toString()).
  }

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 2023-03-13
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多