Created
February 14, 2024 18:44
-
-
Save mshivam019/dadb99c4b49c9693c783cbd6fc21fe90 to your computer and use it in GitHub Desktop.
Zustand Store with react native mmkv example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {create} from 'zustand'; | |
| import {persist} from 'zustand/middleware'; | |
| import {createJSONStorage} from 'zustand/middleware'; | |
| import {MMKV} from 'react-native-mmkv'; | |
| const storage = new MMKV(); | |
| const zustandStorage = { | |
| setItem: (name, value) => { | |
| return storage.set(name, value); | |
| }, | |
| getItem: (name) => { | |
| const value = storage.getString(name); | |
| return value ?? null; | |
| }, | |
| removeItem: (name) => { | |
| return storage.delete(name); | |
| }, | |
| }; | |
| const useFlagStore = create( | |
| persist( | |
| (set) => ({ | |
| flag: false, | |
| setFlag: (state) => { | |
| set({ | |
| flag: state, | |
| }); | |
| }, | |
| }), | |
| { | |
| name: 'some-flag-storage', | |
| storage: createJSONStorage(() => zustandStorage), | |
| }, | |
| ), | |
| ); | |
| export default useFlagStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment