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
}
}
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'),
};
}
来源:https://stackoverflow.com/questions/35866005/mapstatetoprops-must-return-an-object-instead-received-map