Skip to content

Commit 94df67d

Browse files
committed
Discard useRefCallback
1 parent 2f0e431 commit 94df67d

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
],
1010
"rules": {
1111
"react-hooks/rules-of-hooks": "error",
12-
"react-hooks/exhaustive-deps": "warn",
12+
"react-hooks/exhaustive-deps": ["warn", {
13+
"additionalHooks": "(useRefEffect)" // Use `|` to add more hooks. "(useX|useY|useZ)"
14+
}],
1315
"@typescript-eslint/no-explicit-any": "off",
1416
"@typescript-eslint/explicit-function-return-type": "off",
1517
"@typescript-eslint/no-use-before-define": "off",
@@ -20,6 +22,7 @@
2022
"@typescript-eslint/explicit-module-boundary-types": "off",
2123
"react/prop-types": "off",
2224
"react/display-name": "off"
25+
2326
},
2427
"settings": {
2528
"react": {

src/cloud/lib/hooks/index.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
import { useState, useRef, useEffect, useCallback } from 'react'
1+
import { useState, useRef, useEffect } from 'react'
22

33
export function useRefState<S>(initialValue: S) {
44
const [state, setState] = useState<S>(initialValue)
5-
const stateRef = useRef(state)
6-
useEffect(() => {
7-
stateRef.current = state
8-
}, [state])
5+
const stateRef = useRefEffect(state)
6+
97
return [state, stateRef, setState] as [
108
S,
119
React.MutableRefObject<S>,
1210
React.Dispatch<React.SetStateAction<S>>
1311
]
1412
}
1513

16-
export function useRefCallback<T extends (...args: any[]) => any>(
17-
callback: T,
18-
deps: React.DependencyList
19-
) {
20-
/* eslint-disable react-hooks/exhaustive-deps */
21-
const cb = useCallback(callback, deps)
22-
23-
const callbackRef = useRef(cb)
14+
export function useRefEffect<S>(value: S) {
15+
const valueRef = useRef(value)
2416

2517
useEffect(() => {
26-
callbackRef.current = cb
27-
}, [cb])
18+
valueRef.current = value
19+
}, [value])
2820

29-
return [cb, callbackRef] as [T, React.MutableRefObject<T>]
21+
return valueRef
3022
}
3123

3224
export function useCommittedRef<T>(value: T): React.MutableRefObject<T> {

src/cloud/lib/stores/pageStore/store.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
useMemo,
99
} from 'react'
1010
import { PageDataContext, PageDataProps } from './types'
11-
import { useCommittedRef, useRefCallback } from '../../hooks'
11+
import { useCommittedRef, useRefEffect } from '../../hooks'
1212
import { SerializedUserTeamPermissions } from '../../../interfaces/db/userTeamPermissions'
1313
import { SerializedTeam } from '../../../interfaces/db/team'
1414
import { SerializedUser } from '../../../interfaces/db/user'
@@ -32,7 +32,7 @@ function usePageDataStore(pageProps: any) {
3232
setPageData(pageProps)
3333
}, [pageProps])
3434

35-
const [setPartialPageData, setPartialPageDataRef] = useRefCallback(
35+
const setPartialPageData = useCallback(
3636
(val: any) => {
3737
setPageData((prevState: any) => {
3838
return Object.assign(
@@ -44,6 +44,7 @@ function usePageDataStore(pageProps: any) {
4444
},
4545
[setPageData]
4646
)
47+
const setPartialPageDataRef = useRefEffect(setPartialPageData)
4748

4849
const team: undefined | SerializedTeam = pageData.team
4950
const permissions: undefined | SerializedUserTeamPermissions[] =

0 commit comments

Comments
 (0)