【发布时间】:2019-07-18 14:37:03
【问题描述】:
我正在尝试使用 react-redux 更新状态,但状态没有被更新。 新价值即将到来 “如果(action.type === 'SET_LOGGED_IN')” 在 reducer 中,但不将 isLoggedIn 更新为 true。 哪里错了? find the code
Login.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;
【问题讨论】:
-
你在哪里发货?
-
请分享
mapStateToProps函数 -
在我的 Login.js 文件中,请检查'mapDispatchToProps'
-
也请分享
mapStateToProps,mapDispatchToProps正在添加功能。 -
@VladimirBogomolov 添加了codesandbox.io/s/148rm8qy9q
标签: javascript reactjs redux react-redux redux-form