Skip to content

Instantly share code, notes, and snippets.

@Leechael
Last active February 16, 2021 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leechael/e2f4aa67d76080615d3faee3245bef25 to your computer and use it in GitHub Desktop.
Save Leechael/e2f4aa67d76080615d3faee3245bef25 to your computer and use it in GitHub Desktop.
one-time restore initial from async func.
import { atom } from "jotai";
import AsyncStorage from "@react-native-community/async-storage";
const jwtTokenAtom = atom<string, string>("", (_, set, value) => {
set(jwtTokenAtom, value);
AsyncStorage.setItem("jwt_token", value);
});
jwtTokenAtom.onMount = (set) => {
(async () => {
const value = await AsyncStorage.getItem("jwt_token");
if (value) {
set(value);
}
})();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment