reactjs Zustand从另一家Zustand商店获得状态

svmlkihl  于 2023-01-30  发布在  React
关注(0)|答案(2)|浏览(342)
  • 我在react应用程序中创建了2个Zustand存储,一个称为UserStore,另一个称为SettingsStore,如何将状态从UserStore获取到SettingsStore?*
import create from 'zustand';

const UserStore = (set, get) => ({

user: {},

});
  • 我想将此“user”变量用于名为SettingsStore的其他存储 *
import create from 'zustand';

const SettingsStore = (set, get) => ({


});
ogsagwnx

ogsagwnx1#

必须在SettingStore文件中导入UserStore,然后使用UserStore.getState().user

kpbwa7wx

kpbwa7wx2#

您可以在另一个存储区中导入存储区,并通过.getState()函数检索状态。

import create from "zustand";

const useUserState = create((set, get) => ({
  userId: undefined,
}));

const useSettingState = create((set, get) => ({
  updateSetting: (user) => {
    const userId = useUserState.getState().userId;
    // do something with userId
  },
}));

相关问题