您好,我使用不可变映射作为状态,当我尝试 mapStateToProps 时,出现此错误。
未捕获的不变违规:mapStateToProps
必须返回一个对象。
而是收到了 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
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
}
}
这个 github 问题涵盖了这个确切的问题:https://github.com/reactjs/react-redux/issues/60 https://github.com/reactjs/react-redux/issues/60.
您可以在 mapStateToProps 函数中手动从地图中提取所需的值:
const mapStateToProps = (state) => {
return {
constraints: state.get('constraints'),
selectedAmount: state.get('selectedAmount'),
selectedTerm: state.get('selectedTerm'),
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)