我已经设置了减速器,动作和存储,它们似乎工作正常。但是,当我尝试通过this.props访问商店时,我得到了未定义的对象。知道为什么会这样吗?
Index.js:
class Index extends React.Component {
render() {
console.log("##########################")
console.log(this.props)
this.props.dispatchloggedInUser("User found");
console.log(this.props.dispatchGetUser())
console.log("###########################")
return <Root/>;
}
}
function mapDispatchToProps(dispatch) {
return {
dispatchloggedInUser: (data) => dispatch(loggedInUser(data)),
dispatchGetUser: () => dispatch(getUser())
}
}
function mapStateToProps(state) {
return {
user: state.user
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Index)
AuthReducer:
switch (action.type) {
case LOGGED_IN: {
console.log("Inside Logged IN")
console.log(state)
return [
...state,
{
user: action.data
}
]
}
case GET_USER: {
return {...state }
}
default:
return { ...state }
答案 0 :(得分:0)
看看你的root reducer - 用户可能嵌套在auth
或类似的东西 - 找出:
function mapStateToProps(state) {
console.log(state);
return {
user: state.auth.user // or ?
}
}
答案 1 :(得分:0)
我认为您应该更改此部分:
case LOGGED_IN: {
console.log("Inside Logged IN")
console.log(state)
return [
...state,
{
user: action.data
}
]
}
到...
case LOGGED_IN: {
console.log("Inside Logged IN")
console.log(state)
let user = action.data
return {...state, user}
}