Reactjs mapStateToProps必须返回一个对象。相反,收到了映射{}?
您好,我使用状态的Immuteble映射,当我尝试maspStateToProps时,出现了此错误 未捕获不变冲突:Reactjs mapStateToProps必须返回一个对象。相反,收到了映射{}?,reactjs,redux,immutable.js,react-redux,Reactjs,Redux,Immutable.js,React Redux,您好,我使用状态的Immuteble映射,当我尝试maspStateToProps时,出现了此错误 未捕获不变冲突:mapStateToProps必须返回一个对象。 相反,收到了映射{} 这是我的密码: 组成部分: const mapStateToProps = (state) => { return state } class LoanCalculator extends React.Component{ componentWillM
mapStateToProps
必须返回一个对象。
相反,收到了映射{}
这是我的密码:
组成部分:
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
此github问题涵盖了以下问题: 您可以在MapStateTrops函数中手动从地图中提取所需的值:
const mapStateToProps = (state) => {
return {
constraints: state.get('constraints'),
selectedAmount: state.get('selectedAmount'),
selectedTerm: state.get('selectedTerm'),
};
}
您的状态似乎是来自immutable.js的映射,而mapStateToProps应该返回一个对象,正如您的错误消息所示。尝试通过转换映射或将所需的值提取到对象结构来返回对象。为什么不干脆
返回{state}
?这就是我需要的。顺便说一句,这也迫使你记住你要传递的确切内容。在某个时刻,很明显您需要拆分一个不断增长的组件。
const mapStateToProps = (state) => {
return {
constraints: state.get('constraints'),
selectedAmount: state.get('selectedAmount'),
selectedTerm: state.get('selectedTerm'),
};
}