This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
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.
feat : TickerContext for updating data when action commited
- Loading branch information
Showing
4 changed files
with
88 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { createContext, useContext, ReactNode, useReducer } from "react"; | ||
|
||
type TickerType = { | ||
ticker: number; | ||
}; | ||
|
||
const TickerContext = createContext<TickerType | undefined>(undefined); | ||
|
||
type TickerActionType = { | ||
type: "UPDATE_TICKER"; | ||
}; | ||
|
||
const TickerDispatchContext = createContext< | ||
React.Dispatch<TickerActionType> | undefined | ||
>(undefined); | ||
|
||
const TickerReducer = (state: TickerType, action: TickerActionType) => { | ||
switch (action.type) { | ||
case "UPDATE_TICKER": | ||
return { ticker: state.ticker + 1 }; | ||
default: | ||
throw new Error(`Unhandled action type: ${action.type} in TickerReducer`); | ||
} | ||
}; | ||
|
||
type TickerProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
export const TickerProvider = ({ children }: TickerProviderProps) => { | ||
const [state, dispatch] = useReducer(TickerReducer, { | ||
ticker: 0, | ||
}); | ||
return ( | ||
<TickerContext.Provider value={state}> | ||
<TickerDispatchContext.Provider value={dispatch}> | ||
{children} | ||
</TickerDispatchContext.Provider> | ||
</TickerContext.Provider> | ||
); | ||
}; | ||
|
||
export const useTicker = () => { | ||
const context = useContext(TickerContext); | ||
if (context === undefined) { | ||
throw new Error("useTicker must be used within a TickerProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export const useTickerDispatch = () => { | ||
const context = useContext(TickerDispatchContext); | ||
if (context === undefined) { | ||
throw new Error("useTickerDispatch must be used within a TickerProvider"); | ||
} | ||
return context; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.