-
-
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: Stablise props that change in expensive components Most of the work here is memoising props to expensive components like `FlatList`, and `StoryView`, and also using `React.memo` on components to avoid rendering them at all in circumstances that don't involve them. * 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. --------- Co-authored-by: Jonathan Jacobs <[email protected]> Co-authored-by: Daniel Williams <[email protected]>
- Loading branch information
1 parent
9b07b0f
commit 3ec4216
Showing
8 changed files
with
135 additions
and
71 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,43 @@ | ||
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); | ||
} | ||
|
||
/** | ||
* Hook that reads the value of a specific story context parameter. | ||
*/ | ||
export function useStoryContextParam<T = any>(name: string, defaultValue?: T): T { | ||
const paramAtom = useMemo(() => atom(get => get(storyContextAtom)?.parameters?.[name]), [name]); | ||
return useAtomValue(paramAtom) ?? defaultValue; | ||
} | ||
|
||
/** | ||
* 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
Oops, something went wrong.