forked from rupali-codes/LinksHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalContext.tsx
42 lines (38 loc) · 865 Bytes
/
GlobalContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useReducer, createContext, ReactNode } from 'react'
import { IContext } from 'types'
import GlobalReducer from './GlobalReducer'
const initialState: IContext = {
sidebar: false,
}
const GlobalContext = createContext<IContext>(initialState)
const GlobalProvider = ({ children }: { children: ReactNode }) => {
const [state, dispatch] = useReducer(GlobalReducer, initialState)
function openNav() {
dispatch({
type: 'OPEN_NAV',
})
}
function closeNav() {
dispatch({
type: 'CLOSE_NAV',
})
}
function toggleNav() {
dispatch({
type: 'TOGGLE_NAV',
})
}
return (
<GlobalContext.Provider
value={{
sidebar: state.sidebar,
openNav,
closeNav,
toggleNav,
}}
>
{children}
</GlobalContext.Provider>
)
}
export { GlobalContext, GlobalProvider }