This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid extra render on sidebar navigation change
# Conflicts: # frontend/packages/app/utils/navigation-container.tsx # frontend/packages/app/utils/navigation.tsx
- Loading branch information
1 parent
024dec6
commit d8f5b56
Showing
4 changed files
with
60 additions
and
35 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
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 |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import {useSyncExternalStore} from 'react' | ||
import type {StateStream} from '@mintter/shared/src/utils/stream' | ||
import {useEffect, useState, useSyncExternalStore} from 'react' | ||
|
||
export function useStream<V>(stream: StateStream<V>): V { | ||
export function useStream<StreamValue>( | ||
stream: StateStream<StreamValue>, | ||
): StreamValue { | ||
return useSyncExternalStore( | ||
(onStoreChange) => { | ||
return stream.subscribe(onStoreChange) | ||
}, | ||
() => stream.get(), | ||
) | ||
} | ||
|
||
export function useStreamSelector<StreamValue, InternalValue>( | ||
stream: StateStream<StreamValue>, | ||
selector: (value: StreamValue) => InternalValue, | ||
): InternalValue { | ||
const [state, setState] = useState(selector(stream.get())) | ||
useEffect(() => { | ||
return stream.subscribe(() => { | ||
setState(selector(stream.get())) | ||
}) | ||
}, [stream, selector]) | ||
return state | ||
} |