为什么调度操作会重写我的商店价值?

Why does dispatching action rewrite my store value?

我使用 redux hooks 从我的功能组件分派到 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 中,你总是需要 return 当前状态的副本。那就是问题所在。您发送操作的方式没有问题。

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;
};