forked from reck1ess/next-realworld-example-app
-
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
Showing
6 changed files
with
92 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
import React from "react"; | ||
|
||
import useSessionStorage from "../hooks/useSessionStorage"; | ||
|
||
export type PageDispatch = React.Dispatch<any>; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
const PageStateContext = React.createContext<number | undefined>(undefined); | ||
|
||
const PageDispatchContext = React.createContext<PageDispatch | undefined>( | ||
undefined | ||
); | ||
|
||
const PageContextProvider = ({ children }: Props) => { | ||
const [page, setPage] = useSessionStorage("offset", 0); | ||
return ( | ||
<PageDispatchContext.Provider value={setPage}> | ||
<PageStateContext.Provider value={page}> | ||
{children} | ||
</PageStateContext.Provider> | ||
</PageDispatchContext.Provider> | ||
); | ||
}; | ||
|
||
export const usePageState = () => { | ||
const state = React.useContext(PageStateContext); | ||
if (!state) throw new Error("PageContextProvider not found"); | ||
return state; | ||
}; | ||
|
||
export const usePageDispatch = () => { | ||
const dispatch = React.useContext(PageDispatchContext); | ||
if (!dispatch) throw new Error("PageContextProvider not found"); | ||
return dispatch; | ||
}; | ||
|
||
export default PageContextProvider; |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
import React from "react"; | ||
|
||
export type PageCountDispatch = React.Dispatch<any>; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
const PageCountStateContext = React.createContext<number | undefined>( | ||
undefined | ||
); | ||
|
||
const PageCountDispatchContext = React.createContext< | ||
PageCountDispatch | undefined | ||
>(undefined); | ||
|
||
const PageCountContextProvider = ({ children }: Props) => { | ||
const [pageCount, setPageCount] = React.useState(1); | ||
return ( | ||
<PageCountDispatchContext.Provider value={setPageCount}> | ||
<PageCountStateContext.Provider value={pageCount}> | ||
{children} | ||
</PageCountStateContext.Provider> | ||
</PageCountDispatchContext.Provider> | ||
); | ||
}; | ||
|
||
export const usePageCountState = () => { | ||
const state = React.useContext(PageCountStateContext); | ||
if (!state) throw new Error("PageCountContextProvider not found"); | ||
return state; | ||
}; | ||
|
||
export const usePageCountDispatch = () => { | ||
const dispatch = React.useContext(PageCountDispatchContext); | ||
if (!dispatch) throw new Error("PageCountContextProvider not found"); | ||
return dispatch; | ||
}; | ||
|
||
export default PageCountContextProvider; |
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
import React from "react"; | ||
|
||
import PageContext from "./PageContext"; | ||
import PageCountContext from "./PageCountContext"; | ||
|
||
const ContextProvider = ({ children }) => ( | ||
<PageContext> | ||
<PageCountContext>{children}</PageCountContext> | ||
</PageContext> | ||
); | ||
|
||
export default ContextProvider; |