npm-pkg-hook 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +13 -13
- package/.eslintrc.json +107 -107
- package/.github/pull_request_template.md +18 -0
- package/.github/workflows/pepeline.yaml +30 -0
- package/README.md +1 -1
- package/jsconfig.json +27 -27
- package/next.config.js +128 -128
- package/package.json +10 -2
- package/src/cookies/index.ts +3 -3
- package/src/hooks/index.js +19 -15
- package/src/hooks/useAcumulateDate/index.js +15 -15
- package/src/hooks/useAnimationText/index.jsx +29 -29
- package/src/hooks/useCategoryStore/index.js +7 -7
- package/src/hooks/useCategoryStore/queries.js +16 -16
- package/src/hooks/useCheckbox/index.js +114 -0
- package/src/hooks/useClients/index.js +13 -0
- package/src/hooks/useClients/queries.js +118 -0
- package/src/hooks/useDrag/index.js +56 -56
- package/src/hooks/useEvent/index.js +33 -33
- package/src/hooks/useFetchJson/index.js +24 -24
- package/src/hooks/useFetchMoreInteractions/index.jsx +34 -34
- package/src/hooks/useFormTools/index.js +70 -70
- package/src/hooks/useFullScreenMode/index.js +65 -65
- package/src/hooks/useGetCategorieStore/index.js +20 -20
- package/src/hooks/useGetCategorieStore/queries.js +77 -77
- package/src/hooks/useGetProductsFood/index.js +46 -0
- package/src/hooks/useGetProductsFood/queriesStore.js +766 -0
- package/src/hooks/useHover/index.js +28 -28
- package/src/hooks/useInnerHtml/index.js +37 -37
- package/src/hooks/useIntersection/index.js +30 -30
- package/src/hooks/useKeypress/index.js +27 -27
- package/src/hooks/useLocalSorage/index.js +35 -35
- package/src/hooks/useLocationNavigate/index.js +54 -54
- package/src/hooks/useMobile/index.js +38 -0
- package/src/hooks/useRestaurant/index.js +19 -19
- package/src/hooks/useRestaurant/queries.js +69 -69
- package/src/hooks/useSales/index.js +489 -0
- package/src/hooks/useSales/queries.js +230 -0
- package/src/hooks/useSetState/index.js +24 -24
- package/src/hooks/useStore/index.js +18 -0
- package/src/hooks/useStore/queries.js +136 -0
- package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
- package/src/hooks/useUpdateCart/index.js +124 -123
- package/src/hooks/useUser/index.js +3 -3
- package/src/hooks/useWindowSize/index.js +37 -37
- package/src/utils/index.js +54 -27
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
|
|
3
|
-
export const useSetState = initialState => {
|
|
4
|
-
const [state, setState] = useState(initialState)
|
|
5
|
-
const increase = () => {return setState(state + 1)}
|
|
6
|
-
const decrease = () => {return setState(state - 1)}
|
|
7
|
-
const reset = () => {return setState(0)}
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
if (state === -1) return reset()
|
|
10
|
-
return {}
|
|
11
|
-
}, [state])
|
|
12
|
-
// Cambio de estado
|
|
13
|
-
const changeState = () => {
|
|
14
|
-
setState(!state)
|
|
15
|
-
}
|
|
16
|
-
return {
|
|
17
|
-
state,
|
|
18
|
-
increase,
|
|
19
|
-
decrease,
|
|
20
|
-
reset,
|
|
21
|
-
changeState,
|
|
22
|
-
setState
|
|
23
|
-
}
|
|
24
|
-
}
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export const useSetState = initialState => {
|
|
4
|
+
const [state, setState] = useState(initialState)
|
|
5
|
+
const increase = () => {return setState(state + 1)}
|
|
6
|
+
const decrease = () => {return setState(state - 1)}
|
|
7
|
+
const reset = () => {return setState(0)}
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (state === -1) return reset()
|
|
10
|
+
return {}
|
|
11
|
+
}, [state])
|
|
12
|
+
// Cambio de estado
|
|
13
|
+
const changeState = () => {
|
|
14
|
+
setState(!state)
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
state,
|
|
18
|
+
increase,
|
|
19
|
+
decrease,
|
|
20
|
+
reset,
|
|
21
|
+
changeState,
|
|
22
|
+
setState
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useQuery } from '@apollo/client'
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { GET_ONE_STORE } from './queries'
|
|
4
|
+
|
|
5
|
+
export const useStore = () => {
|
|
6
|
+
const [store, setStore] = useState({})
|
|
7
|
+
const { loading, error } = useQuery(GET_ONE_STORE, {
|
|
8
|
+
fetchPolicy: 'cache-and-network',
|
|
9
|
+
onCompleted: (res) => {
|
|
10
|
+
const { getStore } = res || {}
|
|
11
|
+
setStore(getStore)
|
|
12
|
+
},
|
|
13
|
+
onError: (err) => {
|
|
14
|
+
console.log(err)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
return [store, { loading, error }]
|
|
18
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { gql } from '@apollo/client'
|
|
2
|
+
|
|
3
|
+
export const CREATE_ONE_STORE = gql`
|
|
4
|
+
mutation newRegisterStore($input: IStore){
|
|
5
|
+
newRegisterStore(input: $input){
|
|
6
|
+
success
|
|
7
|
+
message
|
|
8
|
+
idStore
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
`
|
|
12
|
+
export const GET_ONE_STORE = gql`
|
|
13
|
+
query getStore($id: ID){
|
|
14
|
+
getStore(id: $id ){
|
|
15
|
+
cId
|
|
16
|
+
id
|
|
17
|
+
dId
|
|
18
|
+
idStore
|
|
19
|
+
ctId
|
|
20
|
+
neighborhoodStore
|
|
21
|
+
Viaprincipal
|
|
22
|
+
catStore
|
|
23
|
+
storeOwner
|
|
24
|
+
storeName
|
|
25
|
+
ImageName
|
|
26
|
+
emailStore
|
|
27
|
+
storePhone
|
|
28
|
+
socialRaz
|
|
29
|
+
Image
|
|
30
|
+
banner
|
|
31
|
+
|
|
32
|
+
documentIdentifier
|
|
33
|
+
uPhoNum
|
|
34
|
+
storeName
|
|
35
|
+
ULocation
|
|
36
|
+
upLat
|
|
37
|
+
upLon
|
|
38
|
+
uState
|
|
39
|
+
siteWeb
|
|
40
|
+
|
|
41
|
+
description
|
|
42
|
+
createdAt
|
|
43
|
+
secVia
|
|
44
|
+
NitStore
|
|
45
|
+
typeRegiments
|
|
46
|
+
typeContribute
|
|
47
|
+
addressStore
|
|
48
|
+
pais {
|
|
49
|
+
cId
|
|
50
|
+
cName
|
|
51
|
+
cCalCod
|
|
52
|
+
cState
|
|
53
|
+
cDatCre
|
|
54
|
+
cDatMod
|
|
55
|
+
}
|
|
56
|
+
city {
|
|
57
|
+
ctId
|
|
58
|
+
dId
|
|
59
|
+
cName
|
|
60
|
+
cState
|
|
61
|
+
cDatCre
|
|
62
|
+
cDatMod
|
|
63
|
+
}
|
|
64
|
+
department {
|
|
65
|
+
dId
|
|
66
|
+
cId
|
|
67
|
+
dName
|
|
68
|
+
dState
|
|
69
|
+
dDatCre
|
|
70
|
+
dDatMod
|
|
71
|
+
}
|
|
72
|
+
getStoreSchedules {
|
|
73
|
+
idStore
|
|
74
|
+
schId
|
|
75
|
+
id
|
|
76
|
+
schDay
|
|
77
|
+
schHoSta
|
|
78
|
+
schHoEnd
|
|
79
|
+
schState
|
|
80
|
+
}
|
|
81
|
+
cateStore {
|
|
82
|
+
catStore
|
|
83
|
+
cName
|
|
84
|
+
cState
|
|
85
|
+
cDatCre
|
|
86
|
+
cDatMod
|
|
87
|
+
csDescription
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
`
|
|
93
|
+
export const GET_ONE_STORE_BY_ID = gql`
|
|
94
|
+
query getOneStore($idStore: ID){
|
|
95
|
+
getOneStore(idStore: $idStore ){
|
|
96
|
+
cId
|
|
97
|
+
id
|
|
98
|
+
dId
|
|
99
|
+
idStore
|
|
100
|
+
ctId
|
|
101
|
+
neighborhoodStore
|
|
102
|
+
Viaprincipal
|
|
103
|
+
catStore
|
|
104
|
+
storeOwner
|
|
105
|
+
storeName
|
|
106
|
+
emailStore
|
|
107
|
+
storePhone
|
|
108
|
+
socialRaz
|
|
109
|
+
Image
|
|
110
|
+
banner
|
|
111
|
+
documentIdentifier
|
|
112
|
+
uPhoNum
|
|
113
|
+
ULocation
|
|
114
|
+
upLat
|
|
115
|
+
upLon
|
|
116
|
+
uState
|
|
117
|
+
siteWeb
|
|
118
|
+
description
|
|
119
|
+
secVia
|
|
120
|
+
NitStore
|
|
121
|
+
typeRegiments
|
|
122
|
+
typeContribute
|
|
123
|
+
addressStore
|
|
124
|
+
createAt
|
|
125
|
+
cateStore {
|
|
126
|
+
catStore
|
|
127
|
+
cName
|
|
128
|
+
cState
|
|
129
|
+
cDatCre
|
|
130
|
+
cDatMod
|
|
131
|
+
csDescription
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
`
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
|
|
3
|
-
const DATE_UNITS = [
|
|
4
|
-
['day', 86400],
|
|
5
|
-
['hour', 3600],
|
|
6
|
-
['minute', 60],
|
|
7
|
-
['second', 1]
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
const getDateDiffs = (timestamp) => {
|
|
11
|
-
const now = Date.now()
|
|
12
|
-
const elapsed = (timestamp - now) / 1000
|
|
13
|
-
|
|
14
|
-
for (const [unit, secondsInUnit] of DATE_UNITS) {
|
|
15
|
-
if (Math.abs(elapsed) > secondsInUnit || unit === 'second') {
|
|
16
|
-
const value = Math.round(elapsed / secondsInUnit)
|
|
17
|
-
return { value, unit }
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const useTimeAgo = (timestamp) => {
|
|
23
|
-
const [timeAgo, setTimeAgo] = useState(() => { return getDateDiffs(timestamp) })
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
const interval = setInterval(() => {
|
|
27
|
-
const newTimeAgo = getDateDiffs(timestamp)
|
|
28
|
-
setTimeAgo(newTimeAgo)
|
|
29
|
-
}, 5000)
|
|
30
|
-
|
|
31
|
-
return () => { return clearInterval(interval) }
|
|
32
|
-
}, [timestamp])
|
|
33
|
-
|
|
34
|
-
const rtf = new Intl.RelativeTimeFormat('es', { style: 'short' })
|
|
35
|
-
|
|
36
|
-
const { value, unit } = timeAgo
|
|
37
|
-
|
|
38
|
-
return rtf && rtf?.format(value, unit)
|
|
39
|
-
}
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
const DATE_UNITS = [
|
|
4
|
+
['day', 86400],
|
|
5
|
+
['hour', 3600],
|
|
6
|
+
['minute', 60],
|
|
7
|
+
['second', 1]
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
const getDateDiffs = (timestamp) => {
|
|
11
|
+
const now = Date.now()
|
|
12
|
+
const elapsed = (timestamp - now) / 1000
|
|
13
|
+
|
|
14
|
+
for (const [unit, secondsInUnit] of DATE_UNITS) {
|
|
15
|
+
if (Math.abs(elapsed) > secondsInUnit || unit === 'second') {
|
|
16
|
+
const value = Math.round(elapsed / secondsInUnit)
|
|
17
|
+
return { value, unit }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const useTimeAgo = (timestamp) => {
|
|
23
|
+
const [timeAgo, setTimeAgo] = useState(() => { return getDateDiffs(timestamp) })
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const interval = setInterval(() => {
|
|
27
|
+
const newTimeAgo = getDateDiffs(timestamp)
|
|
28
|
+
setTimeAgo(newTimeAgo)
|
|
29
|
+
}, 5000)
|
|
30
|
+
|
|
31
|
+
return () => { return clearInterval(interval) }
|
|
32
|
+
}, [timestamp])
|
|
33
|
+
|
|
34
|
+
const rtf = new Intl.RelativeTimeFormat('es', { style: 'short' })
|
|
35
|
+
|
|
36
|
+
const { value, unit } = timeAgo
|
|
37
|
+
|
|
38
|
+
return rtf && rtf?.format(value, unit)
|
|
39
|
+
}
|
|
@@ -1,123 +1,124 @@
|
|
|
1
|
-
import debounce from 'lodash.debounce'
|
|
2
|
-
import { useState } from 'react'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
1
|
+
import debounce from 'lodash.debounce'
|
|
2
|
+
import { useState, useEffect } from 'react'
|
|
3
|
+
import { getCurrentDomain } from '../../utils'
|
|
4
|
+
import { trigger } from '../useEvent'
|
|
5
|
+
import { Cookies } from '../../cookies'
|
|
6
|
+
|
|
7
|
+
// EXAMPLE
|
|
8
|
+
// https://codesandbox.io/s/nextjs-cart-system-tfg1e?file=/pages/_app.js
|
|
9
|
+
|
|
10
|
+
// Method to execute the event to add all items of the app.cart cookie
|
|
11
|
+
const updateCart = debounce((items = []) => {
|
|
12
|
+
trigger({ eventType: 'app.cart', data: { loading: true, items } })
|
|
13
|
+
}, 3000)
|
|
14
|
+
|
|
15
|
+
const EMPTY_CART = {
|
|
16
|
+
items: [],
|
|
17
|
+
total: 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const useUpdateCart = () => {
|
|
21
|
+
const domain = getCurrentDomain()
|
|
22
|
+
const keyToSaveData = 'app.cart'
|
|
23
|
+
const saveDataState = JSON.parse(Cookies.get(keyToSaveData) || '[]')
|
|
24
|
+
const [cart, setCart] = useState(EMPTY_CART)
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
// restore cart from cookie, this could also be tracked in a db
|
|
28
|
+
const cart = Cookies.get(keyToSaveData)
|
|
29
|
+
|
|
30
|
+
// if items in cart, set items and total to state
|
|
31
|
+
if (typeof cart === 'string' && cart !== 'undefined') {
|
|
32
|
+
const cartData = JSON.parse(cart)
|
|
33
|
+
const total = cartData.reduce(
|
|
34
|
+
(total, item) => {return total + item.price * item.quantity},
|
|
35
|
+
0
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
setCart({ items: cartData, total })
|
|
39
|
+
}
|
|
40
|
+
}, [])
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
Cookies.set(keyToSaveData, JSON.stringify(cart.items), { domain, path: '/' })
|
|
44
|
+
}, [cart, domain])
|
|
45
|
+
|
|
46
|
+
const handleAdd = (item) => {
|
|
47
|
+
// check for item already in cart
|
|
48
|
+
// if not in cart, add item else if item is found increment quantity
|
|
49
|
+
const itemExists = cart.items.find((i) => {return i.pId === item.pId})
|
|
50
|
+
|
|
51
|
+
if (!itemExists) {
|
|
52
|
+
setCart((prevCart) => {return {
|
|
53
|
+
items: [...prevCart.items, { ...item, quantity: 1 }],
|
|
54
|
+
total: prevCart.total + item.price
|
|
55
|
+
}})
|
|
56
|
+
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setCart((prevCart) => {return {
|
|
61
|
+
items: prevCart.items.map((i) => {
|
|
62
|
+
if (i.pId === item.pId) {
|
|
63
|
+
return { ...i, quantity: i.quantity + 1 }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return i
|
|
67
|
+
}),
|
|
68
|
+
total: prevCart.total + item.price
|
|
69
|
+
}})
|
|
70
|
+
updateCart(cart)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const deleteProductCart = (item) => {
|
|
74
|
+
setCart((prevCart) => {
|
|
75
|
+
const items = prevCart.items
|
|
76
|
+
const index = items.findIndex((i) => {return i.pId === item.pId})
|
|
77
|
+
|
|
78
|
+
items.splice(index, 1)
|
|
79
|
+
|
|
80
|
+
const total = items.reduce((t, i) => {return t + i.quantity * i.price}, 0)
|
|
81
|
+
|
|
82
|
+
return { items, total }
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const decreaseItemFromCart = (item) => {
|
|
87
|
+
// check for item already in cart
|
|
88
|
+
// if quantity is more then in cart, subtract item else remove item
|
|
89
|
+
const itemInCart = cart.items.find((i) => {return i.pId === item.pId})
|
|
90
|
+
|
|
91
|
+
if (!itemInCart) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (itemInCart.quantity === 1) {
|
|
96
|
+
deleteProductCart(item)
|
|
97
|
+
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setCart((prevCart) => {return {
|
|
102
|
+
items: prevCart.items.map((i) => {
|
|
103
|
+
if (i.pId === item.pId) {
|
|
104
|
+
return { ...i, quantity: item.quantity - 1 }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return i
|
|
108
|
+
}),
|
|
109
|
+
total: prevCart.total - item.price
|
|
110
|
+
}})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const clearCart = () => {
|
|
114
|
+
setCart(EMPTY_CART)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
saveDataState,
|
|
119
|
+
clearCart,
|
|
120
|
+
deleteProductCart,
|
|
121
|
+
decreaseItemFromCart,
|
|
122
|
+
handleAdd
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const useUser = () => {
|
|
2
|
-
return []
|
|
3
|
-
}
|
|
1
|
+
export const useUser = () => {
|
|
2
|
+
return []
|
|
3
|
+
}
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { useState, useEffect } from 'react';
|
|
2
|
-
|
|
3
|
-
export default function useWindowSize() {
|
|
4
|
-
// Initialize state with undefined width/height so server and client renders match
|
|
5
|
-
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
|
|
6
|
-
const [windowSize, setWindowSize] = useState({
|
|
7
|
-
width: undefined,
|
|
8
|
-
height: undefined
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
// Handler to call on window resize
|
|
13
|
-
function handleResize() {
|
|
14
|
-
// Set window width/height to state
|
|
15
|
-
setWindowSize({
|
|
16
|
-
width: window.innerWidth,
|
|
17
|
-
height: window.innerHeight
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Add event listener
|
|
22
|
-
window.addEventListener('resize', handleResize);
|
|
23
|
-
|
|
24
|
-
// Call handler right away so state gets updated with initial window size
|
|
25
|
-
handleResize();
|
|
26
|
-
|
|
27
|
-
// Remove event listener on cleanup
|
|
28
|
-
return () => window.removeEventListener('resize', handleResize);
|
|
29
|
-
}, []); // Empty array ensures that effect is only run on mount
|
|
30
|
-
|
|
31
|
-
return windowSize;
|
|
32
|
-
}
|
|
33
|
-
// Como se usa
|
|
34
|
-
|
|
35
|
-
// 1 -const size = useWindowSize();
|
|
36
|
-
// 2 <div>
|
|
37
|
-
// {size.width}px / {size.height}px
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export default function useWindowSize() {
|
|
4
|
+
// Initialize state with undefined width/height so server and client renders match
|
|
5
|
+
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
|
|
6
|
+
const [windowSize, setWindowSize] = useState({
|
|
7
|
+
width: undefined,
|
|
8
|
+
height: undefined
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
// Handler to call on window resize
|
|
13
|
+
function handleResize() {
|
|
14
|
+
// Set window width/height to state
|
|
15
|
+
setWindowSize({
|
|
16
|
+
width: window.innerWidth,
|
|
17
|
+
height: window.innerHeight
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Add event listener
|
|
22
|
+
window.addEventListener('resize', handleResize);
|
|
23
|
+
|
|
24
|
+
// Call handler right away so state gets updated with initial window size
|
|
25
|
+
handleResize();
|
|
26
|
+
|
|
27
|
+
// Remove event listener on cleanup
|
|
28
|
+
return () => window.removeEventListener('resize', handleResize);
|
|
29
|
+
}, []); // Empty array ensures that effect is only run on mount
|
|
30
|
+
|
|
31
|
+
return windowSize;
|
|
32
|
+
}
|
|
33
|
+
// Como se usa
|
|
34
|
+
|
|
35
|
+
// 1 -const size = useWindowSize();
|
|
36
|
+
// 2 <div>
|
|
37
|
+
// {size.width}px / {size.height}px
|
|
38
38
|
// </div>
|