为什么我的 redux 状态没有更新
why my redux state not updating
state not updating.when action is dispatched state should update to isAuthenticated to true..but the state not updating..redux 返回初始状态而不是更新后的状态。
export function setCurrentUser(user) {
console.log(user);
return {
type: "SET_CURRENT_USER",
user
};
}
...
export function login(userData) {
return dispatch => {
return axios.post('/user/auth',userData).then((res) => {
const { status, sessionId, username, error} = res.data;
if(status === "success"){
dispatch(setCurrentUser(sessionId));
}else {
dispatch(invalidUser(error));
}
});
}
}
//reducers
import { SET_CURRENT_USER, INVALID_USER } from "../actions/types";
import isEmpty from "lodash/isEmpty";
const initialState = {
isAuthenticated : false,
user: {},
error:{}
};
export default (state = initialState, action) => {
switch(action.type){
case SET_CURRENT_USER:
return {
...state,
isAuthenticated: !isEmpty(action.user),
user:action.user
}
...
default: return state;
}
}
//组件
onSubmit(e){
e.preventDefault();
this.setState({ errors: {}, isLoading:true });
this.props.login(this.state).then(
(res) => {
console.log(this.props.userData);
if(this.props.userData.isAuthenticated)
this.context.router.push("greet");
},
(err) => this.setState({ 错误: err.response.data, isLoading: false })
);
}
.....
function mapStateToProps(state) {
return {
userData: state.authReducers
};
}
export default connect(mapStateToProps, { login })(LoginForm);
您没有更新减速器中的负载数据
export default (state = initialState, action) => {
switch(action.type){
case SET_CURRENT_USER:
return {...state, user: action.payload} //update user here
}
...
default: return state;
}
}
以上代码使用了 es6 特性,假设 isAuthenticated
和 error
是初始状态的一部分。
你只是返回相同的 JSON 这就是你的状态没有更新的原因。
state not updating.when action is dispatched state should update to isAuthenticated to true..but the state not updating..redux 返回初始状态而不是更新后的状态。
export function setCurrentUser(user) {
console.log(user);
return {
type: "SET_CURRENT_USER",
user
};
}
...
export function login(userData) {
return dispatch => {
return axios.post('/user/auth',userData).then((res) => {
const { status, sessionId, username, error} = res.data;
if(status === "success"){
dispatch(setCurrentUser(sessionId));
}else {
dispatch(invalidUser(error));
}
});
}
}
//reducers
import { SET_CURRENT_USER, INVALID_USER } from "../actions/types";
import isEmpty from "lodash/isEmpty";
const initialState = {
isAuthenticated : false,
user: {},
error:{}
};
export default (state = initialState, action) => {
switch(action.type){
case SET_CURRENT_USER:
return {
...state,
isAuthenticated: !isEmpty(action.user),
user:action.user
}
...
default: return state;
}
}
//组件
onSubmit(e){
e.preventDefault();
this.setState({ errors: {}, isLoading:true });
this.props.login(this.state).then(
(res) => {
console.log(this.props.userData);
if(this.props.userData.isAuthenticated)
this.context.router.push("greet");
},
(err) => this.setState({ 错误: err.response.data, isLoading: false }) );
}
.....
function mapStateToProps(state) {
return {
userData: state.authReducers
};
}
export default connect(mapStateToProps, { login })(LoginForm);
您没有更新减速器中的负载数据
export default (state = initialState, action) => {
switch(action.type){
case SET_CURRENT_USER:
return {...state, user: action.payload} //update user here
}
...
default: return state;
}
}
以上代码使用了 es6 特性,假设 isAuthenticated
和 error
是初始状态的一部分。
你只是返回相同的 JSON 这就是你的状态没有更新的原因。