Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Can';无法从redux访问状态对象_Reactjs_Redux - Fatal编程技术网

Reactjs Can';无法从redux访问状态对象

Reactjs Can';无法从redux访问状态对象,reactjs,redux,Reactjs,Redux,因此,我试图访问从后端服务器返回的值。数据在Redux中应该进行更新,但不知何故,我无法在render函数中访问它。 问题是,使用this.props.account返回和对象。但是当我试图访问它里面的任何内容时,例如this.props.account.role它会说未定义。但是前面显示的对象应该是USER 这是我的密码 import React, {Component} from 'react'; import {Loading} from '../components/helpers/Lo

因此,我试图访问从后端服务器返回的值。数据在Redux中应该进行更新,但不知何故,我无法在render函数中访问它。 问题是,使用
this.props.account
返回和对象。但是当我试图访问它里面的任何内容时,例如
this.props.account.role
它会说未定义。但是前面显示的对象应该是USER

这是我的密码

import React, {Component} from 'react';
import {Loading} from '../components/helpers/Loading.component';
import {Navbar} from '../components/navigation/Navigation.component';
import { connect } from 'react-redux';
import {fetchAccount} from '../actions/index.action';

export class Account extends Component {

    componentDidMount() {
        this.props.fetchAccount;
    }

    render() {
        return (
            <Loading isLoading={!this.props.account}>
                <Navbar />
                {console.log(this.props.account)}
            </Loading>
        );
    }
}

const mapStateToProps = state => ({
    account: state.account.account
});

const mapDispatchToProps = dispatch => ({
    fetchAccount: dispatch(fetchAccount())
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Account)
减速器

export const account = (state = [], action) => {
switch(action.type) {
    case 'GET_ACCOUNT':
        return {
            ...state,
            account: action.payload
        };
    default:
        return state; 
    }
}
降根剂

import { tickets } from './ticket.reducer';
import { account } from './account.reducer';
import {combineReducers} from 'redux';

export default combineReducers({
    tickets,
    account
});

对于您在问题中描述的内容,您正在尝试访问如下对象数组:

accounts = [
 {
  name: 'Jimbo',
  id: 1
  role: 'player',
 },
 {
  name: 'James',
  id: 2,
  role: 'employee'
 },
]
当您声明
此.props.account.role
时。您正在尝试访问一个确实不存在的属性

您可以尝试过滤数组(A)、要访问的特定索引(B)或通过数组映射(C)

A:
this.props.accounts.find(account=>acount.role===“player”)

B:
this.props.accounts[0]。角色


C:
this.props.accounts.map(role=>{li>{account.role})
对于其他到达此帖子的人,请检查以确保在状态实际更新之前没有尝试钻取有效负载。-这就是我的问题。

在返回
render()
之前,放入
console.log(this.props.account)
,并告诉我们您得到了什么。此外,您还缺少
componentWillReceiveProps
,它将允许组件在承诺得到解决后获取新数据,而我这样做时,它将返回undefined。如果我使用console.log this.props,我可以看到account对象,但无法进入其中。我应该在receiveProps中做什么,只是添加它还是需要一些代码?它实际上不是一个数组,它只是一个包含一个用户的对象,所以我觉得奇怪的是,我无法以这种方式访问它。使用
object.kets(this.props.accounts)
,怎么样,这将返回
this.props.accounts
的所有可枚举props的列表。当我执行
对象.keys(this.props)
时,我会看到该帐户。当我在帐户上执行此操作时,控制台会记录此错误:
无法将未定义或null转换为object
我认为这可能与我在操作中执行
data.json()
有关,因此它只是一个普通的json对象。但是执行
JSON.parse(data)
将表明它已经是一个对象。我尝试通过执行
console.log(action.payload.userId)
在reducer中访问它,效果非常好。这就导致了view和reducerIt之间的联系,它可能与组件的类型和绑定有关。当您
consle.log(this.props)
时,它会显示什么?您能在
render()
方法上尝试
console.log(JSON.stringify(this.props,null,2))
并与我共享吗?
accounts = [
 {
  name: 'Jimbo',
  id: 1
  role: 'player',
 },
 {
  name: 'James',
  id: 2,
  role: 'employee'
 },
]