Skip to content

Instantly share code, notes, and snippets.

@yangli-io
Last active February 13, 2022 12:59
Show Gist options
  • Select an option

  • Save yangli-io/97aa25bd81d4d25915c57036b8691f98 to your computer and use it in GitHub Desktop.

Select an option

Save yangli-io/97aa25bd81d4d25915c57036b8691f98 to your computer and use it in GitHub Desktop.
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