为什么调度操作会重写我的存储值



我使用redux钩子从功能组件向redux存储区调度了两次。

当我像这样单独调度这些时,只有一个存储在我的redux存储中,因为它似乎每次都会刷新,并且只保留一个。如何将它们一起调度,或者如何防止redux存储刷新并丢失第一个调度负载

dispatch({
type: "access_token",
payload: googleUser.accessToken,
});
dispatch({
type: "firebase_userId",
payload: result.user.uid,
});

Redux商店

import React from "react";
import symbolicateStackTrace from "react-native/Libraries/Core/Devtools/symbolicateStackTrace";
import { applyMiddleware, createStore } from "redux";
import thunk from "redux-thunk";
const initialState = {
access_token: "",
firebase_userId: "",
};
const counterReducer = (
state = initialState,
action
) => {


if (action.type === "access_token") {

return {

access_token: action.payload,
};
}
if (action.type === "firebase_userId") {

return {

firebase_userId: action.payload,
};
}
return state;
};
const store = createStore(counterReducer, applyMiddleware(thunk));
export default store;

在reducer中,您总是需要返回当前状态的副本。这就是问题所在。调度操作的方式没有错。

const counterReducer = (
state = initialState,
action
) => {
if (action.type === "access_token") {

return {
// copy & update state
...state,
access_token: action.payload,
};
}
if (action.type === "firebase_userId") {

return {
// copy & update state
...state,
firebase_userId: action.payload,
};
}
return state;
};

相关内容

  • 没有找到相关文章

最新更新