mapStateToProps must return an object. Instead received Map {}?

Grund picture Grund · Mar 8, 2016 · Viewed 24.1k times · Source

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

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

Here is my code:

Component:

    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

REDUCER

    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
      }
    }

Answer

Calvin Belden picture Calvin Belden · Mar 8, 2016

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

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'),
  };
}