简体   繁体   English

React-Redux容器在Connect(ModalRoot)中抛出“ mapStateToProps()”时必须返回一个普通对象。 而是收到未定义的信息。”

[英]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()必须返回普通对象。相反,接收到的是未定义的对象。”


Here is the code for the modal container and the code that calls it: 这是模式容器的代码以及调用它的代码:

 // 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) 


Its probably something simple i'm forgetting but its driving me crazy, any comments or suggestions welcome. 这可能是我忘记的简单事情,但它使我发疯,欢迎任何评论或建议。

According to redux implementation and example you should write like this 根据redux的实现和示例,您应该这样编写

export default connect(
  state => ({
    modal: state.modal
  })
)(ModalRoot)

See here 这里

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.

相关问题 应用程序有效,但会产生错误:Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 而是收到未定义 - Aplication works but generates an error: mapDispatchToProps() in Connect(App) must return a plain object. Instead received undefined Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 而是收到了[object Promise] - mapDispatchToProps() in Connect(App) must return a plain object. Instead received [object Promise] 动作必须是普通对象。 相反,实际类型是:&#39;undefined&#39;。 React-Redux - Actions must be plain objects. Instead, the actual type was: 'undefined'. React-Redux 如何修复错误:“您必须将组件传递给连接返回的 function。 而是在我的 React-Redux 应用程序中收到 {}”? - How to fixed error: “You must pass a component to the function returned by connect. Instead received {}”, in my React-Redux app? 理解 React-Redux 和 mapStateToProps() - Understanding React-Redux and mapStateToProps() 链接connect / mapStateToProps / mapDispatchToProps函数以在react-redux中重用代码 - Chain connect/mapStateToProps/mapDispatchToProps functions for code reuse in react-redux React + Redux connect(mapStateToProps) - React + Redux connect (mapStateToProps) REACT-REDUX:未捕获错误:操作必须是普通对象 - REACT-REDUX : Uncaught Error: Actions must be plain objects 在react-redux中的reducers之后不会触发mapStateToProps - `mapStateToProps` is not triggered after reducers in react-redux 如何使用react-redux连接mapStateToProps,MapDispatchToProps和redux-router - How use react-redux connect with mapStateToProps,MapDispatchToProps and redux-router
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM