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
}
}
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.
In class components mapStateToProps works the same as functional components, I feel there are only differences in some syntax or calling approaches.
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.
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.
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'),
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With