简体   繁体   English

反应 Redux 减速器不更新 State

[英]React Redux reducer not Updating State

So I have a reducer which doesn't seem to be updating the state at all whenever I called the 'LOGIN' action.所以我有一个减速器,每当我调用“登录”操作时,它似乎根本不会更新 state。 It could be a problem with my React Redux code.这可能是我的 React Redux 代码的问题。 It's either my component is not getting re rendered whenever the store's state changes or the reducer is not changing the stores state at all.每当商店的 state 更改时,我的组件都不会重新呈现,或者减速器根本没有更改商店 state。

Reducer ------减速器------

const initialState = {
    messages: [],
    loginDetails: {
        email: '',
        password: ''
    },
    activeUsers: [],
    loginActive: false
}

const messageReducer = (state = initialState, action) => {
    switch(action.type) {
        case 'ADD_MESSAGE': 
            if(state.messages.length < 50) {
                let newStateMessages = [...state.messages]
                newStateMessages.unshift(action.payload);
                console.log(newStateMessages);
                return {...state, messages: newStateMessages};
            } else {
                let newStateMessages = [...state.messages]
                newStateMessages.pop();
                newStateMessages.unshift(action.payload);
                return {...state, newStateMessages};
            }
        case 'LOGIN':
            console.log('LOGIN');
            console.log(action);
            const newLoginDetails = {
                email: action.payload.email,
                password: action.payload.password
            };
            console.log({...state, loginDetails: newLoginDetails});
            return {...state, loginDetails: newLoginDetails};
        case 'UPDATE_USERS':
            const newActiveUsers = action.payload;
            return {...state, activeUsers: newActiveUsers};
        case 'LOGIN_ACTIVE':
            return {...state, loginActive: true};
        case 'LOGIN_EXIT':
            return {...state, loginActive: false};
        default:
            return state;
    }
}

export const store = createStore(messageReducer);

React Redux connect -----反应 Redux 连接 -----

    const mapStateToProps = state => {
      return { ...state }
    }

    export default connect(mapStateToProps)(Home);

This mapStateToProps returns...这个 mapStateToProps 返回...

{
   activeUsers: []
   dispatch: ƒ dispatch(action)
   loginActive: true
   loginDetails: {email: "", password: ""}
   messages: []
   __proto__: Object
}

when it should return...什么时候应该回来...

{
   activeUsers: []
   loginActive: true
   loginDetails: {email: "example@gmail.com", password: 
   "password"}
   messages: []
   __proto__: Object
}

I have tested for sure that the dispatch to the reducer is getting called, and the payload is correct.我已经测试过,确保调用了 reducer 的调度,并且有效负载是正确的。 However, the reducer is failing to update the state with the LOGIN action type.但是,reducer 无法使用 LOGIN 操作类型更新 state。

Can you try this:你能试试这个吗:

const mapStateToProps = ({activeUsers,loginActive,loginDetails,messages}) => ({
   activeUsers,
   loginActive,
   loginDetails,
   messages
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM