Skip to content

Instantly share code, notes, and snippets.

View vanxh's full-sized avatar
👨‍💻
vanxh.dev

Vanxh vanxh

👨‍💻
vanxh.dev
View GitHub Profile
@DistractionBoy
DistractionBoy / createCtx.tsx
Last active August 11, 2022 19:39
quick react context with reducer
export function createCtx<StateType, ActionType>(
reducer: React.Reducer<StateType, ActionType>,
initialState: StateType,
) {
const defaultDispatch: React.Dispatch<ActionType> = () => initialState // we never actually use this
const ctx = React.createContext({
state: initialState,
dispatch: defaultDispatch, // just to mock out the dispatch type and make it not optioanl
})
function Provider(props: React.PropsWithChildren<{}>) {