[英]React-Redux container throws “mapStateToProps() in Connect(ModalRoot) must return a plain object. Instead received undefined.”
I am building a Redux based Model/Dialog trigger based on Dan Abramov's solution to this question: Dan Abramov's solution 我正在基于Dan Abramov的以下问题的解决方案构建基于Redux的模型/对话框触发器: Dan Abramov的解决方案
The error I am getting is "mapStateToProps() in Connect(ModalRoot) must return a plain object. Instead received undefined." 我收到的错误是“ Connect(ModalRoot)中的mapStateToProps()必须返回普通对象。相反,接收到的是未定义的对象。”
// The Modal Container
import LoginModal from '../components/Modals/LoginModal'
import {connect} from 'react-redux'
const MODAL_COMPONENTS = {
'LOGIN_MODAL': LoginModal
/* other modals */
}
const ModalRoot = ({ modalType, modalProps }) => {
if (!modalType) {
return <span /> // after React v15 you can return null here
}
const SpecificModal = MODAL_COMPONENTS[modalType]
return <SpecificModal {...modalProps} />
}
export default connect(
state => state.modal
)(ModalRoot)
// The Modal Container import LoginModal from '../components/Modals/LoginModal' import {connect} from 'react-redux' const MODAL_COMPONENTS = { 'LOGIN_MODAL': LoginModal /* other modals */ } const ModalRoot = ({ modalType, modalProps }) => { if (!modalType) { return <span /> // after React v15 you can return null here } const SpecificModal = MODAL_COMPONENTS[modalType] return <SpecificModal {...modalProps} /> } export default connect( state => state.modal )(ModalRoot)
Perhaps mapStateToProps needs to pick a specific object from your reducer 也许mapStateToProps需要从化简器中选择一个特定的对象
Could you try something like this? 你可以尝试这样的事情吗?
function mapStateToProps(state){
const {modalType,modalProps} = state.modal;
return {modalType,modalProps};
}
Its either this or perhaps you arent properly passing combineReducers to your store 这或者您可能没有正确地将CombineReducers传递到您的商店
The root cause of the problem is that state.modal
is undefined. 问题的根本原因是
state.modal
未定义。 You can get more info by adding a simple log statement like: 您可以通过添加以下简单的日志语句来获取更多信息:
export default connect(state => { console.log('state', state); return state.modal })(ModalRoot)
This may help you see what's in state
and help in figuring out why state.modal
is undefined. 这可以帮助您查看
state
并帮助弄清为什么未定义state.modal
。
To make this error go away, you can use either of these 2 solutions: 要使此错误消失,可以使用以下两种解决方案之一:
Ensuring the object is not undefined: 确保对象不是未定义的:
export default connect(state => state.modal || {})(ModalRoot)
Using ES6 destructuring: 使用ES6解构:
export default connect(({ modal }) => ({ modal }))(ModalRoot)
If you use the ES6 destructuring (which is essentially same as @MichaelRasoahaingo's solution), your prop destructuring will also change to: 如果您使用ES6解构(与@MichaelRasoahaingo的解决方案基本相同),则您的道具解构也将更改为:
const ModalRoot = ({ modal: { modalType, modalProps } }) => {
Or you can use this alternative syntax and keep your destructuring as is: 或者,您可以使用这种替代语法,并按原样保留解构:
export default connect(({ modal }) => ({ ...modal }))(ModalRoot)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.