组件未获取对Redux存储的更改

时间:2019-02-03 04:55:13

标签: reactjs redux react-redux

我有一个无法从商店中获取数据的组件。

class UserList extends PureComponent {

    constructor(props) {
        super(props);
        this.state = ({
            users: []
        })
    }

    componentWillReceiveProps (newProps) {
        console.log("New Props", newProps);
        this.setState({
            users: newProps
        })
    }

    render() {
        return (
            <div>
                TEST
            </div>
        )
    }
}

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({ 'editUser': editUser, 'deleteUser': deleteUser }, dispatch);
}

const mapStateToProps = (state) => {
    console.log("State.users", state.users);
    return {
        users: state.users
    }
};

export default connect(mapDispatchToProps, mapStateToProps)(UserList);

我在这里有一个减速器

let originalUsers = [...state.users];
console.log("originalUsers", originalUsers);
switch(action.type) {
    case "ADD_USER":
        originalUsers.push(action.payload);
        return {users: originalUsers};

Reducer正在获取并返回正确的数据,但是mapStateToProps中我的UserList组件中的行;当我登录“ stage.users”时,它一直处于未定义状态,我不明白为什么。

1 个答案:

答案 0 :(得分:1)

您的

export default connect(mapDispatchToProps, mapStateToProps)(UserList);

应更正为

export default connect(mapStateToProps, mapDispatchToProps)(UserList);

mapStateToProps是connect函数中in的第一个参数。