React Redux reducer未更新状态



所以我有一个reducer,每当我调用"LOGIN"操作时,它似乎根本不会更新状态。这可能是我的React Redux代码的问题。要么是我的组件在商店的状态发生变化时没有被重新渲染,要么是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连接-----

const mapStateToProps = state => {
return { ...state }
}
export default connect(mapStateToProps)(Home);

此mapStateToProps返回。。。

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

什么时候应该回来。。。

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

我已经进行了测试,以确保对reducer的调度被调用,并且有效负载是正确的。但是,reducer无法使用LOGIN操作类型更新状态。

你能试试这个吗:

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

相关内容

  • 没有找到相关文章

最新更新