[英]React/Redux: Why are my my properties in this.props undefined?
I am trying to set up Redux in React for the first time and I can't seem to pass my initial state from the store to the component. 我试图第一次在React中设置Redux,但似乎无法将初始状态从存储传递到组件。 My store file is setting state to the return value of the reducer.
我的商店文件正在将状态设置为减速器的返回值。 Here is what happens when I log this.props to the console
这是将this.props登录到控制台时发生的情况
Component 零件
import React from 'react'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import { exampleAction } from '../../actions'; class Header extends React.Component { constructor(props) { super(props); this.state = {} } render() { console.log(this.props) return ( <div> <p>this is {this.props.examplePropOne}</p> </div> ); } } const mapStateToProps = state => ({ examplePropOne: state.examplePropOne, examplePropTwo: state.examplePropTwo }); const mapDispatchToProps = dispatch => { return bindActionCreators({ exampleAction }, dispatch) } export default connect(mapStateToProps, mapDispatchToProps)(Header);
Reducer 减速器
import { EXAMPLE_ACTION } from './../actions/types' const initialState = { examplePropOne : 'Example Property One', examplePropTwo : 'Example Property Two' } export default function (state = initialState, action) { switch(action.type) { case EXAMPLE_ACTION: return { ...state, examplePropOne: action.payload } default: return state } }
Action 行动
import { EXAMPLE_ACTION } from './types' export const exampleAction = text => ({ type: EXAMPLE_ACTION, payload: text, })
[Edit] [编辑]
Here is what happens when I log the state within mapStateToProps 这是我在mapStateToProps中记录状态时发生的情况
import React from 'react'; import { createStore, combineReducers } from 'redux'; import reducers from '../reducers'; export const store = createStore( combineReducers({ state: reducers }), );
With how combineReducers()
was used with state
passed in as a key, your mapStateToProps()
would need to look like this instead to access examplePropOne
and examplePropTwo
: 通过将
combineReducers()
与作为键传递的state
一起使用的state
,您的mapStateToProps()
将需要看起来像这样,以访问examplePropOne
和examplePropTwo
:
const mapStateToProps = state => ({
examplePropOne: state.state.examplePropOne,
examplePropTwo: state.state.examplePropTwo
});
Given that combineReducers() : 鉴于CombineReducers() :
The state produced by combineReducers() namespaces the states of each reducer under their keys as passed to combineReducers()
由combinedReducers()产生的状态在传递给combinedReducers()的键名下对每个Reducer的状态进行命名空间。
The issue is that: 问题是:
export const store = createStore(
combineReducers({
state: reducers
}),
);
The key state
passed to combineReducers()
created a namespace/property of state
. 传递给
combineReducers()
的键state
创建了state
的名称空间/属性。 With the argument named state
for the mapStateToProps()
, requires that properties are accessed as state.state
. 使用
mapStateToProps()
名为state
的参数时,要求将属性作为state.state
进行访问。 This can probably be resolved by instead giving the key passed to combineReducers()
a more descriptive name representing what is being used to manage in the store. 可以通过给传递给
combineReducers()
的密钥一个更具描述性的名称来表示,该名称代表商店中要使用的管理内容,可以解决此问题。 For example, if it's related to authentication, it could be called some like auth
. 例如,如果它与身份验证有关,则可以将其称为
auth
。 It would look like: 它看起来像:
export const store = createStore(
combineReducers({
auth: reducers
}),
);
// ...
const mapStateToProps = state => ({
examplePropOne: state.auth.examplePropOne,
examplePropTwo: state.auth.examplePropTwo
});
Hopefully that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.