Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Zustand Recipe to include removal of useEffect dependecy #181

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions docs/recipes/Zustand.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface AuthenticationStoreSnapshot extends SnapshotOut<typeof Authenti

</details>

MobX-State-Tree models declare the data type, initial values, derived values, and actions all in one.
MobX-State-Tree models declare the data type, initial values, derived values, and actions all in one.
Zustand takes a "barebones" approach and defines a store as a basic state object with data and actions co-located.

Create a new file `app/store/AuthenticationStore.ts` and convert the model to Zustand to look like this:
Expand Down Expand Up @@ -535,7 +535,19 @@ const episodeStore = useEpisodeStore();
```

```diff
<Toggle
useEffect(() => {
;(async function load() {
setIsLoading(true)
await episodeStore.fetchEpisodes()
setIsLoading(false)
})()
-- }, [episodeStore])
++ }, [])

```

```diff
<Switch
value={episodeStore.favoritesOnly}
onValueChange={() =>
-- episodeStore.setProp("favoritesOnly", !episodeStore.favoritesOnly)
Expand All @@ -554,7 +566,7 @@ const parsedTitleAndSubtitle = getParsedTitleAndSubtitle(episode);

```diff
<Text
style={$metadataText}
style={themed($metadataText)}
size="xxs"
--accessibilityLabel={episode.datePublished.accessibilityLabel}
++accessibilityLabel={datePublished.accessibilityLabel}
Expand Down Expand Up @@ -647,21 +659,19 @@ We added the `persist` middleware and created `_hasHydrated` property & action t

...

const [areFontsLoaded] = useFonts(customFontsToLoad)
const [areFontsLoaded, fontLoadError] = useFonts(customFontsToLoad)

-const { rehydrated } = useInitialRootStore(() => {
- setTimeout(hideSplashScreen, 500)
-})

-useEffect(() => {
- hideSplashScreen()
-}, [])

+const hasHydrated = useStore((state) => state._hasHydrated)
+const rehydrated = useStore((state) => state._hasHydrated)
+useEffect(() => {
+ if (hasHydrated) {
+ if (rehydrated) {
+ setTimeout(hideSplashScreen, 500)
+ }
+}, [hasHydrated])

-if (!isNavigationStateRestored || !areFontsLoaded) return null
+if (!hasHydrated || !isNavigationStateRestored || !areFontsLoaded) return null
+}, [rehydrated])

```

Expand Down
Loading