gpt4 book ai didi

javascript - reducer 不更新 react redux 中的状态

转载 作者:行者123 更新时间:2023-12-03 16:39:06 36 4
gpt4 key购买 nike

我正在尝试使用 react-redux 更新状态,但状态未更新。新的值(value)正在到来“如果(action.type ==='SET_LOGGED_IN')”在 reducer 中,但不将 isLoggedIn 更新为 true。什么问题? find the code

登录.js

function handleLoginClick(username, password, e) {
e.preventDefault();

post('/createUser', { username, password })
.then(({ status }) => {
if (status === 200) {
console.log(this.props);
this.props.setLoggedIn(true);
this.props.history.push('/');
}else{
this.props.setLoggedIn(false);

}
})
.catch(error => {
.........
})
}
..................
const mapStateToProps = state => {
return {isLoggedIn : state.reducer.isLoggedIn};};
const mapDispatchToProps = dispatch => {
return {setLoggedIn : (value) => dispatch({type: 'SET_LOGGED_IN', value: value}),}};

export default compose(
withStyles(styles),
withRouter,
connect(mapStateToProps, mapDispatchToProps)
)(NewLogin);

reducer.js

import { combineReducers } from 'redux';
import { reducer as reduxFormReducer } from 'redux-form';

const initialStates = {
isLoggedIn : false
};

const reducers = combineReducers({
reducer : (state = initialStates, action) => {
//console.log(action);
if (action.type === 'SET_LOGGED_IN') {
//console.log(action);
return {
...state,
isLoggedIn: action.value
};
}
return state;
},

form: reduxFormReducer, // mounted under "form"
});

export default reducers;

最佳答案

-修正错误-

在我的代码中,状态更新正确。当我访问时,我使用了未定义的 state.isLoggedIn。我从 state.reducer.isLoggedIn 替换了它。现在一切正常。

感谢@UmairFarooq、@VladimirBogomolov 以及所有发表评论并尝试修复它的人。

const mapStateToProps = state => {
return {
isLoggedIn : state.reducer.isLoggedIn
};
};

关于javascript - reducer 不更新 react redux 中的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54862003/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com