简体   繁体   English

如何将 redux state 变量设置为组件 state

[英]How to set redux state variable to component state

I have the below component我有以下组件

export class Requirements extends React.Component {
  static propTypes = {
    locArray: PropTypes.array
  };

  constructor(props) {
    super(props);
    this.state = {
      selectedJ: props.locArray.length !== 0 ? '1' : '2'
    };
    this.updateSelected = this.updateSelected.bind(this);
  }

  updateSelected(e, selectedJ) {
    this.setState({ selectedJ }, () => {
      console.log('this.state.selectedJ:', this.state.selectedJ);
    });
  }

  render() {

    return (
     // CODE
              <Field
                onChange={this.updateSelected}
              />
            <DocTable
              selectedJ={this.state.selectedJ}
            />
    );
  }
}

Im mapping the locArray from the redux state to the component props like below我将 locArray 从 redux state 映射到组件道具,如下所示

const mapStateToProps = (state, props) => ({
  locArray:
    state.role.roleArray?.[props.index].roleData?.rolelocArray || []
});

export default connect(mapStateToProps)(Requirements);

so that i can initialize the state variable selectedJ based on locArray length like below这样我就可以根据 locArray 长度初始化 state 变量 selectedJ,如下所示

constructor(props) {
    super(props);
    this.state = {
      selectedJ: props.locArray.length !== 0 ? '1' : '2'
    };
    this.updateSelected = this.updateSelected.bind(this);
  }

I feel something is wrong with this approach.我觉得这种方法有问题。 I get the below error on the code that's inside the render because of this locArray that i mapped in the mapStateToProps.由于我在 mapStateToProps 中映射的这个 locArray,我在渲染内部的代码上收到以下错误。

Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

my code works as expected without the locArray in the mapStateToProps but i need to get that from redux store and initialize the state variable selectedJ based on this locArray.我的代码在没有 mapStateToProps 中的 locArray 的情况下按预期工作,但我需要从 redux 存储中获取它,并基于此 locArray 初始化 state 变量 selectedJ。

Fairly new to redux, Any thoughts on how to achieve this would help. redux 相当新,任何关于如何实现这一点的想法都会有所帮助。 Thanks谢谢

I fixed this issue.我解决了这个问题。

Issue was with reading rolelocArray from redux state in mapStateToProps问题是从 mapStateToProps 中的 redux state 读取 rolelocArray

const mapStateToProps = (state, props) => ({
  locArray:
    state.role.roleArray?.[props.index].roleData?.rolelocArray || []
});

rolelocArray was not always available. rolelocArray 并不总是可用。 It was being set on a condition in the reducer.它被设置在减速器的条件下。 I thought my ||我以为我的|| [] would work if rolelocArray is not available at any point in time.如果 rolelocArray 在任何时间点都不可用, [] 将起作用。

But it didn't.但它没有。

Instead, I was reading from another variable "rolelocRefArray" that's available available in the redux state.相反,我从 redux state 中可用的另一个变量“rolelocRefArray”中读取。

const mapStateToProps = (state, props) => ({
  locArray:
    state.role.roleArray?.[props.index].roleData?.rolelocRefArray || []
});

Another way of solving this could be having initial value for rolelocArray in the redux state.解决此问题的另一种方法可能是在 redux state 中为 rolelocArray 设置初始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM