diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-30 01:47:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-30 01:47:27 +0300 |
commit | 720a32c4cb1697f3a8f90973f28c334551ab87ff (patch) | |
tree | c9028ea3ff850774d33cbc510eb19dd0e9f7aade /src/hooks/useNavigate.tsx | |
parent | b301bf24c5037403a1e5fc32fc8c10794941b528 (diff) | |
parent | e170d04d5d2c8c86d2683f3accb4feb2d94c881a (diff) | |
download | which-ui-720a32c4cb1697f3a8f90973f28c334551ab87ff.tar.gz |
Merge pull request #54 from which-ecosystem/hooks
Use hooks logic
Diffstat (limited to 'src/hooks/useNavigate.tsx')
-rw-r--r-- | src/hooks/useNavigate.tsx | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/hooks/useNavigate.tsx b/src/hooks/useNavigate.tsx new file mode 100644 index 0000000..befc529 --- /dev/null +++ b/src/hooks/useNavigate.tsx @@ -0,0 +1,41 @@ +import React, { useState, useContext, createContext } from 'react'; + +export interface Page { + prefix: string; + id?: string; +} + +interface ContextType { + page: Page; + setPage: (page: Page) => void; + navigate: (prefix: string, id?: string) => void; +} + +const landingPage = { prefix: 'feed' }; + +const context = createContext<ContextType>({ + page: landingPage, + setPage: () => {}, + navigate: () => {} +}); + +const useProvideNavigation = () => { + const [page, setPage] = useState<Page>(landingPage); + + const navigate: ContextType['navigate'] = (prefix, id?) => { + setPage({ prefix, id }); + }; + + return { page, setPage, navigate }; +}; + +export const NavigationProvider: React.FC = ({ children }) => { + const navigation = useProvideNavigation(); + const { Provider } = context; + return <Provider value={navigation}>{children}</Provider>; +}; + +export const useNavigate = (): ContextType => { + return useContext(context); +}; + |