TypeError: Cannot read property 'contacts' of undefined
TypeError: Cannot read property 'contacts' of undefined
它只显示未定义的联系人。
我有 index.s 这是指向联系 Reducer.js 的主要减速器,我将供应商与我的组件连接 Contact.js
Contacts.js
index.js缅因减速器
触点Reducer.js 触点减速器
我的第一个想法是 mapStateToProps
中的 state.myContact
未定义。确保您正在创建您的商店并将其传递给您的提供商。
文档中的类似内容:https://redux.js.org/recipes/configuring-your-store/#configuring-your-store
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import rootReducer from './reducers' <-- this is your main reducer index.js, where you export the combineReducers
import App from './components/App'
const store = createStore(rootReducer) <--- make sure you have this line
render(
<Provider store={store}> <-- and this line
<App />
</Provider>,
document.getElementById('root')
)
您尝试使用联系人的第二个地方是行
const { contacts } = this.props
如果 this.props
未定义,则无法从中获取 contacts
。
在 Contacts.js 中,您需要一个构造函数来确保定义了 props。添加调用 super(props)
.
的构造函数
constructor(props) {
super(props);
}
我在这里找到了信息:https://reactjs.org/docs/react-component.html#constructor
The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.
这可能仅适用于您尝试在构造函数中使用 props 的情况,因此这可能不是问题,但值得一试。
它只显示未定义的联系人。 我有 index.s 这是指向联系 Reducer.js 的主要减速器,我将供应商与我的组件连接 Contact.js
Contacts.js
我的第一个想法是 mapStateToProps
中的 state.myContact
未定义。确保您正在创建您的商店并将其传递给您的提供商。
文档中的类似内容:https://redux.js.org/recipes/configuring-your-store/#configuring-your-store
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import rootReducer from './reducers' <-- this is your main reducer index.js, where you export the combineReducers
import App from './components/App'
const store = createStore(rootReducer) <--- make sure you have this line
render(
<Provider store={store}> <-- and this line
<App />
</Provider>,
document.getElementById('root')
)
您尝试使用联系人的第二个地方是行
const { contacts } = this.props
如果 this.props
未定义,则无法从中获取 contacts
。
在 Contacts.js 中,您需要一个构造函数来确保定义了 props。添加调用 super(props)
.
constructor(props) {
super(props);
}
我在这里找到了信息:https://reactjs.org/docs/react-component.html#constructor
The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.
这可能仅适用于您尝试在构造函数中使用 props 的情况,因此这可能不是问题,但值得一试。