简体   繁体   English

Redux mapStateToProps返回未定义

[英]Redux mapStateToProps returns undefined

Redux is successfully storing and updating state. Redux已成功存储和更新状态。 The reducers are seemingly working correctly. 减速器似乎正常工作。 I'm able to use this.props.dispatch . 我可以使用this.props.dispatch However, when it actually comes to detailing that information (ie this.props.array I always seem to get undefined. 但是,当涉及到详细说明该信息时(即this.props.array我似乎总是变得不确定。

Reducer: 减速器:

export default function array(state = {}, action) {
    switch (action.type) {
      case "UPDATE_ARRAY":
        state.array = action.array
        return state;
      default:
        return state;
  }
}

State-aware component: 状态感知组件:

  constructor(props) {
    super(props);
    this.state = {
      array: array
    }
  }

-- -

  self.props.dispatch({
    type: 'UPDATE_ARRAY',
    array: array
  })

-- -

const mapStateToProps = (state) => {
      return {
        messages: state.messages,
        array: state.array
      };
    };

    export default connect(mapStateToProps)(Component);

This only seems to be able to save state btw when I define an empty array. 当我定义一个空数组时,这似乎只能保存状态btw。 This doesn't seem right, I thought the intention of Redux was a self-contained store? 这似乎不对,我认为Redux的初衷是一个独立的商店? Updating a variable seems to defeat the purpose a bit. 更新变量似乎会破坏目标。

Would appreciate any help. 将不胜感激。

export default function array(state = {}, action) {
switch (action.type) {
  case "UPDATE_ARRAY":state={
        ...state,
        array:action.array
                   }
    return state;
  default:
    return state;

} } }}

  • you should always update your state immutably,instead of mutating the current application state ,you should create another object and return that.State should be immutable ,only way to change the state is to create a new one.This helps to improve the performance of the application. 您应该始终不变地更新状态,而不是改变当前应用程序状态,而应该创建另一个对象并返回那个状态。状态应该是不可变的,更改状态的唯一方法是创建一个新的状态。这有助于提高的性能。应用程序。
  • I am not sure if you application has more than one reducer or not, if it has, than you must be using combine reducer method .So to access state.array in mapsStateToProps is like this 我不确定您的应用程序是否具有多个reducer,如果具有,则必须使用Combine reducer方法。因此在mapsStateToProps中访问state.array就像这样

    const mapStateToProps = (state) => { return { const mapStateToProps =(state)=> {return {

     messages: state.{reducer_name}.message, array: state.{reducer_name}.array 

    }; }; }; };

  • in place of 'reducer_name' you have to specify the reducers_name which you have define in combine reducer 代替“ reducer_name”,您必须指定您在组合减速器中定义的reducers_name

  • And last mapStateToProps return array ,in props not in component state. 最后一个mapStateToProps返回数组,在props中不处于组件状态。 which you can access in this way {this.props.array},you cant set component state in componentDidMount and in componentWillRecieveProps (in case of aysnc action). 您可以通过{this.props.array}这样的方式进行访问,您不能在componentDidMount和componentWillRecieveProps中设置组件状态(如果aysnc操作)。

I am not sure the following is your issues, but hope these will help: 我不确定以下是您遇到的问题,但希望这些问题会有所帮助:

export default function array(state = {}, {type, array}) {
    switch (type) {
      case "UPDATE_ARRAY":
        return {...state, array};
      default:
        return state;
  }
}

Your reducer should be pure, which you had is mutating the state. 您的reducer应该是纯的,您已经在改变状态。

constructor(props) {
    super(props);
    this.state = {
      array: array // what is array?
    }
}

Above constructor is not right. 上面的构造函数是不对的。 You should be able to access the array from this.props.array as your mapStateToProps 您应该能够从this.props.array作为mapStateToProps访问该array

Do a console.log(this.props) in your render function or ComponentWillReceiveProps , see if you can something :) render函数或ComponentWillReceiveProps执行console.log(this.props) ,看看是否可以console.log(this.props) :)

Your component will receive array as a field in its props field. 您的组件将在其props字段中将array作为字段接收。 Your code assumes it's in the state field. 您的代码假定它在state字段中。 So instead of: 所以代替:

this.state = {
      array: array
    }

you would just access this.props.array wherever in your code you need to use the array. 您只需在代码中需要使用数组的任何地方访问this.props.array You don't need to put it in the local state at all. 您根本不需要将其置于本地状态。 Usually, you would use it in the render function, like in this example: 通常,您可以在render函数中使用它,如以下示例所示:

render()
{
  return <div>The array contains {this.props.array.length} items.</div>
}

I wonder if you're confusing local state with the Redux store's state? 我想知道您是否将本地状态与Redux存储的状态混淆了? Local state is what you get/set when you access this.state in your component code. 本地状态是您在组件代码中访问this.state时获得/设置的状态。 Every component can have its own state object that it can read from and write to. 每个组件都可以有自己的状态对象,可以从中读取和写入数据。

The Redux store's state is what's passed in to mapStateToProps . Redux存储的状态是传递给mapStateToProps 的状态 It's usually the entire state object of all the combined reducers in your top-level reducer (though if you only have one reducer function and are not using combineReducers , then the store state is identical to that single reducer's state). 它通常是顶级减速器中所有组合减速器的整个状态对象(尽管如果您只有一个减速器功能并且未使用combineReducers ,则存储状态与单个减速器的状态相同)。

I suggest choosing more descriptive variable names, so that your code will be more readable. 我建议选择更具描述性的变量名称,以使您的代码更具可读性。 It's hard to understand what your intentions are for your code with such generic names. 用这样的通用名称很难理解您对代码的意图。 For example, you could name your reducer something that indicates what it's for, like bookListReducer , and name the array you want to store and retrieve for what will go inside it, like books . 例如,您可以为reducer命名以表示其用途,例如bookListReducer ,并命名要存储和检索其中将要包含的内容的数组,例如books Naming both your reducer and all your variables array makes it harder to read your code. 命名化简器和所有变量array将使阅读代码更加困难。 This will help anyone who reads your code in the future - including, most importantly, you!, as well as future Stack Overflow readers of your future questions (and perhaps this one if you edit it). 这将对以后阅读您的代码的所有人(包括最重要的是您!)以及将来的Stack Overflow读者(将来的问题(如果您对其进行编辑,也许也是这样))提供帮助。

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

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