-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
H
authored and
H
committed
Jul 19, 2023
1 parent
2a7012f
commit 47b6da1
Showing
3 changed files
with
42 additions
and
7 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,26 @@ | ||
import { createContext, useState, useContext } from 'react'; | ||
|
||
// Create a context | ||
const SingletonContext = createContext(); | ||
|
||
// Create a provider | ||
export function SingletonContextProvider({ children }) { | ||
const [state, setState] = useState('singleton'); | ||
|
||
// make sure state is only created once | ||
let contextValue = useContext(SingletonContext); | ||
if (!contextValue) { | ||
contextValue = { state, setState }; | ||
} | ||
|
||
return ( | ||
<SingletonContext.Provider value={contextValue}> | ||
{children} | ||
</SingletonContext.Provider> | ||
); | ||
} | ||
|
||
// Create a hook to use the SingletonContext, this is just to simplify using the context. | ||
export function useSingletonContext() { | ||
return useContext(SingletonContext); | ||
} |
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