反应redux-thunk组件不会呈现this.props

问题描述:

我是新来的反应者,我想了解如何提出异步ajax请求.我能够完成请求,并将返回的数据添加到我的状态,但是无法将数据呈现到我的组件.这是我的设置.

I'm new to react and I'm trying to understand on to make async ajax request. I was able to get the request completed and the data returned added to my state, but I can't render the data to my component. Here's my setup.

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { getUsers } from '../../actions';

class User extends Component {
    displayName: 'User';

    componentWillMount() {
        this.props.getUsers();
    }

    renderUsers() {
        return this.props.users.map(user => {
            return (
                <h5>{user.Name}</h5>
            );
        });
    }

    render() {
        var component;
        if (this.props.users) {
            component = this.renderUsers()
        } else {
            component = <h3>asdf</h3>;
        }

        return (            
            <div>
                {component}
            </div>

        );
    };
};

function mapStateToProps(state) {
    return {
        users: state.all
    };
}

export default connect(mapStateToProps, { getUsers })(User);

动作

import request from '../helpers/request';
import { GET_USERS } from './types';

export function getUsers() {

    return request.get(GET_USERS, 'Person/GetPeople');
}

使用request.js模块中的get函数

With the get function from request.js module

get: function (action, url) {
        return (dispatch) => {
            axios.get(`${ROOT_URL}${url}`)
                .then(({ data }) => {
                    dispatch({
                        type: action,
                        payload: data
                    });
                });
        };
    }

User_reducer

import { GET_USERS } from '../actions/types';

export default function (state = {}, action) {
    switch (action.type) {
        case GET_USERS:
            console.log(action);
            return { ...state, all: action.payload }
        default:
            return state;
    };
}

当我加载UserPage组件时,可以在redux开发工具中看到请求已完成并且状态正在更新,但是我无法显示this.props.users.

When I load the UserPage component, I can see the request being done and the state being updated in the redux dev tool, but I can't display this.props.users.

如果我在User组件的render()方法中取出了if(this.props.users),则会在this.props.users上得到未定义的内容.

If I take out the if(this.props.users) in the render() method of the User component, I get an undefined on this.props.users.

我在做什么错了?

任何帮助,我们将不胜感激!谢谢

Any help greatly appreciated! Thank you

解决方案是在mapStateToProps函数中将users属性设置为state.users.all.

The solution was to set the users property to state.users.all in the mapStateToProps function.

function mapStateToProps(state) {
    return {
        users: state.users.all
    };
}

谢谢

您可以检查一下,是否在该渲染函数中定义了this.renderUsers()?可能是您忘记了绑定它,而该功能是undefined?

Could you check, do you have this.renderUsers() defined in that render function? It might be you forgot to bind it, and that function is undefined?

尝试在构造函数中添加this.renderUsers = this.renderUsers.bind(this);,看看是否有帮助.

Try to add this.renderUsers = this.renderUsers.bind(this); in your constructor and see, if it helps.