-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Apply state colocation principles and avoid prop drilling
In particular the story context state is maintained near the root of the tree, any changes to this state end up re-rendering a large number of components, such as the story list. By reading the state closer to where it needs to be used, it is possible to avoid prop drilling-inflicted rerenders. I opted for Jotai here, since its a relatively small and well tested and maintained state management library, that imposes basically nothing on an application's existing structure.
- Loading branch information
Jonathan Jacobs
committed
Feb 16, 2023
1 parent
a04e092
commit d877d29
Showing
7 changed files
with
98 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useMemo } from 'react'; | ||
import type { StoryContext } from '@storybook/csf'; | ||
import { atom, useAtomValue, useSetAtom } from 'jotai'; | ||
|
||
import type { ReactNativeFramework } from './types/types-6.0'; | ||
|
||
const storyContextAtom = atom(null as (StoryContext<ReactNativeFramework> | null)); | ||
|
||
/** | ||
* Hook that returns a function to set the current story context. | ||
*/ | ||
export function useSetStoryContext() { | ||
return useSetAtom(storyContextAtom); | ||
} | ||
|
||
/** | ||
* Hook to read the current story context. | ||
*/ | ||
export function useStoryContext() { | ||
return useAtomValue(storyContextAtom); | ||
} | ||
|
||
export function useStoryContextParam<T = any>(name: string, defaultValue?: T): T { | ||
const paramAtom = useMemo( | ||
() => atom(get => get(storyContextAtom)?.parameters?.[name] ?? defaultValue), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[name] | ||
); | ||
return useAtomValue(paramAtom); | ||
} | ||
|
||
/** | ||
* Hook that indicates if `storyId` is the currently selected story. | ||
*/ | ||
export function useIsStorySelected(storyId: string) { | ||
return useAtomValue(useMemo(() => atom(get => get(storyContextAtom)?.id === storyId), [storyId])); | ||
} | ||
|
||
/** | ||
* Hook that indicates if `title` is the currently selected story section. | ||
*/ | ||
export function useIsStorySectionSelected(title: string) { | ||
return useAtomValue(useMemo(() => atom(get => get(storyContextAtom)?.title === title), [title])); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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