mapStateToProps必须返回一个对象.而是收到了地图{}? [英] mapStateToProps must return an object. Instead received Map {}?

查看:216
本文介绍了mapStateToProps必须返回一个对象.而是收到了地图{}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我使用Immuteble Map表示状态,当我尝试使用maspStateToProps时出现此错误.

Hello i use Immuteble Map for state and when i try maspStateToProps i have this error.

未捕获的不变变量:mapStateToProps必须返回一个对象. 而是收到了地图{}.

Uncaught Invariant Violation: mapStateToProps must return an object. Instead received Map {}.

这是我的代码:

组件:

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

     class LoanCalculator extends React.Component{

      componentWillMount(){
       this.dispatch(loadConstraints());
     }

      render(){
        return (
          <div>
            <h1> Loan Calculator </h1>
            <SlidersBox {...this.props}/>
         </div>
       )
     }
   }

    LoanCalculator = connect(
      mapStateToProps
    )(LoanCalculator)

   export default LoanCalculator

减速器

    import { Map } from 'immutable'
    import {LOAD_CONSTRAINTS, SET_AMOUNT_VALUE, SET_TERM_VALUE} from "../actions/actions";

    const initialState = new Map();

    export default function calculator(state = initialState, action){
      switch (action.type){
        case LOAD_CONSTRAINTS:
          return  state.set("constraints", action.constraints)
         case SET_AMOUNT_VALUE:
           return state.set("selectedAmount", action.amount)
        case SET_TERM_VALUE:
         return state.set("selectedTerm", action.term)
        default:
          return state
      }
    }

推荐答案

此github问题涵盖了这个确切的问题: https://github.com/reactjs/react-redux/issues/60 .

This github issue covers this exact problem: https://github.com/reactjs/react-redux/issues/60.

您可以在mapStateToProps函数中从地图中手动提取所需的值:

You can manually extract the values you want from your Map in your mapStateToProps function:

const mapStateToProps = (state) => {
  return {
       constraints: state.get('constraints'),
       selectedAmount: state.get('selectedAmount'),
       selectedTerm: state.get('selectedTerm'),
  };
}

这篇关于mapStateToProps必须返回一个对象.而是收到了地图{}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆