-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-context.js
51 lines (41 loc) · 1.06 KB
/
app-context.js
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
43
44
45
46
47
48
49
50
51
import React, { useContext, useState } from 'react'
const AppContext = React.createContext()
const AppUpdateContext = React.createContext()
export function useAppContext() {
return useContext(AppContext)
}
export function updateAppContext() {
return useContext(AppUpdateContext)
}
export function AppContextProvider({ children }) {
const [activePage, setActivePage] = useState(1)
const [lastPage, setLastPage] = useState(1)
function moveFirst() {
setActivePage((page) => {
return 1
})
}
function movePrev() {
setActivePage((page) => {
return page === 1 ? 1 : page - 1
})
}
function moveNext() {
setActivePage((page) => {
return page === lastPage ? lastPage : page + 1
})
}
function moveLast() {
setActivePage((page) => {
return lastPage
})
}
function setLastPageHandler(val) {
setLastPage(val)
}
return (
<AppContext.Provider value={{ activePage, lastPage }}>
<AppUpdateContext.Provider value={{ moveFirst, movePrev, moveNext, moveLast, setLastPageHandler }}>{children}</AppUpdateContext.Provider>
</AppContext.Provider>
)
}