简体   繁体   English

在 mapStateToProps 中未定义 Redux 状态

[英]Redux state is undefined in mapStateToProps

I am currently following this tutorial.我目前正在关注教程。 I've hit a bit of a snag involving mapStateToProps in the following code:我在以下代码中遇到了一些涉及mapStateToProps的问题:

import React from 'react';
import Voting from './voting';
import {connect} from 'react-redux';

const mapStateToProps = (state) => {
  return {
    pair: state.getIn(['vote','pair']),
    winner: state.get('winner')
  };
}

const VotingContainer = connect(mapStateToProps)(Voting);
export default VotingContainer;

Here is the Voting component that's imported:这是导入的投票组件:

import React from 'react';
import Vote from './Vote';
import Winner from './winner';

const Voting = ({pair,vote,hasVoted,winner}) =>
  <div>
    {winner ? <Winner winner={winner}/>  :
      <Vote pair={pair} vote={vote} hasVoted={hasVoted}/>
    }
  </div>

export default Voting;

It is supposed to render two buttons from the pair prop.它应该从pair道具渲染两个按钮。 The vote prop is a function that will be executed on click, hasVoted disables buttons when true and winner only renders the winner component as shown. vote道具是一个将在点击时执行的函数, hasVoted在 true 时禁用按钮,而获胜者仅呈现如图所示的获胜者组件。

The state is expected to be an immutableJS map that looks like this:状态应该是一个不可变的JS映射,如下所示:

Map({
  vote:{
    pair:List.of('Movie A','Movie B')
  }
});

Instead I am getting an error saying that state is undefined in the state.getIn line.相反,我收到一条错误消息,指出 state.getIn 行中的 state 未定义。

The code setting the state is in index:设置状态的代码在索引中:

const store = createStore(reducer);

const socket = io(document.location.protocol + '//' + document.location.hostname + ':8090');
socket.on('state', state => store.dispatch({
  type: 'SET_STATE',
  state
}));

I have logged store.getState() after setting and it is as expected but undefined in mapStateToProps .我在设置后记录了store.getState() ,它是预期的,但在mapStateToProps undefined I also logged the state variable in above context and it's also as expected.我还在上面的上下文中记录了状态变量,它也符合预期。

I also set the state normally and it surprisingly works!:我也正常设置了状态,它出人意料地有效!:

store.dispatch({
  type: 'SET_STATE',
  state: {
    vote: {
      pair: ['Movie A', 'Movie B']
    }
  }
});

The value of state above is exactly what is received from the server上面 state 的值正是从服务器收到的

Lastly here's what my reducer looks like:最后这是我的减速器的样子:

import React from 'react';
import {Map, fromJS} from 'immutable';

const reducer = (state = Map(), action) => {
  switch (action.type) {
    case 'SET_STATE':
      return state.merge(action.state);
  }
}

export default reducer;

What am I doing wrong?我做错了什么?

EDIT: I realised that mapStateToProps is not being called after the store.dispatch() .编辑:我意识到mapStateToProps后不被称为store.dispatch() I went through the docs for the possible reasons mapStateToProps is not being called and it's not one of them.我浏览了文档,原因mapStateToPropsmapStateToProps没有被调用,而且它不是其中之一。

You reducer doesn't have a default action in switch statement.您的减速器在 switch 语句中没有默认操作。 Which is why even though you mentioned the initial state in reducer params, undefined is returned as store initial state这就是为什么即使您在 reducer params 中提到了初始状态, undefined 也会作为 store 初始状态返回

import React from 'react';
import {Map,fromJS} from 'immutable';

const reducer = (state = Map() ,action) => {
  switch(action.type){
    case 'SET_STATE': return state.merge(action.state);
    default:
      return state;
  }
}

export default reducer;

Adding the default statement will fix the issue :)添加默认语句将解决问题:)

I ended up here too because I had failed to pass my rootreducer function to my createStore method.我也在这里结束了,因为我没有将我的 rootreducer 函数传递给我的createStore方法。 I had:我有:

const store = createStore(applyMiddleware(...middlewares));

I needed:我需要:

const store = createStore(rootReducer(), applyMiddleware(...middlewares));

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

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