Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
      }
    }
like image 580
Grund Avatar asked Mar 08 '16 11:03

Grund


People also ask

What does mapStateToProps return?

Your mapStateToProps function should return a plain object that contains the data the component needs: Each field in the object will become a prop for your actual component. The values in the fields will be used to determine if your component needs to re-render.

Can we use mapStateToProps in class component?

In class components mapStateToProps works the same as functional components, I feel there are only differences in some syntax or calling approaches.

What is mapStateToProps and mapDispatchToProps in react Redux?

The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.

What is ownProps in mapStateToProps?

The mapStateToProps(state, ownProps) is specified as the first argument of connect() call and its ownProps parameter receives the props object of the wrapper component. If the ownProps parameter is not present, React Redux skips calling the function at the props change.


1 Answers

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'),
  };
}
like image 106
Calvin Belden Avatar answered Oct 16 '22 17:10

Calvin Belden