Last active
February 13, 2022 12:59
-
-
Save yangli-io/97aa25bd81d4d25915c57036b8691f98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { createContext, useContext, FC, ReactNode, useState } from 'react' | |
| interface State { | |
| loading: boolean, | |
| currentRound: number, | |
| } | |
| interface IContext { | |
| state: State, | |
| setState: React.Dispatch<React.SetStateAction<State>> | |
| } | |
| const initialState = { | |
| loading: true, | |
| currentRound: 0 | |
| } | |
| const Context = createContext<IContext>({ | |
| state: initialState, | |
| setState: () => {}, | |
| }) | |
| interface Props { | |
| children: ReactNode | |
| } | |
| export const useContextData = () => { | |
| const { state, setState } = useContext(Context); | |
| return { | |
| state, | |
| setState | |
| }; | |
| } | |
| export const ContextDataProvider: FC<Props> = ({ children }) => { | |
| const [state, setState] = useState<State>(initialState) | |
| const context = { | |
| state, | |
| setState, | |
| } | |
| return ( | |
| <Context.Provider value={context}> | |
| {children} | |
| </Context.Provider> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment