mapStateToProps必须返回一个对象而是收到了地图{}?

【字号: 日期:2024-04-19浏览:14作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决mapStateToProps必须返回一个对象而是收到了地图{}??这个github问题涵盖了这个确切的问题: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’), };}解决方法

您好,我将Immuteble Map用作状态,并且当我尝试maspStateToProps时出现此错误。

未捕获的不变违反: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

减速器

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 } }

相关文章: