Created
April 18, 2022 05:35
-
-
Save Josscii/aceccdfd802101d8db4d8b676265d849 to your computer and use it in GitHub Desktop.
how to use context with children to avoid rerender
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
import GlobalSpinner from '@/components/GlobalSpinner' | |
import { contextFactory } from './helpers/contextFactory' | |
import { useToggleState } from '@/hooks/useToggleState' | |
type GlobalSpinnerContextProviderProps = { | |
children: React.ReactNode | |
} | |
const GlobalSpinnerContextProvider = ( | |
props: GlobalSpinnerContextProviderProps | |
) => { | |
const { children } = props | |
const { | |
state: isSpinnerVisible, | |
open: showSpinner, | |
close: hideSpinner, | |
toggle: toggleSpinner, | |
} = useToggleState(false) | |
return ( | |
<GlobalSpinnerContext.Provider | |
value={{ | |
isSpinnerVisible, | |
showSpinner, | |
hideSpinner, | |
toggleSpinner, | |
}} | |
> | |
{children} | |
<GlobalSpinner /> | |
</GlobalSpinnerContext.Provider> | |
) | |
} | |
export default GlobalSpinnerContextProvider |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment