Skip to content

Commit 70c085f

Browse files
gaojudehuozhi
authored andcommitted
clean up useReducer code re dev indicator (#77354)
Follow-up to #76976 Since we want to make explicit mode check from outside, we can now remove the implicitly checked one from inside.
1 parent f99caa6 commit 70c085f

File tree

3 files changed

+34
-52
lines changed

3 files changed

+34
-52
lines changed

packages/next/src/client/components/react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator-internal.tsx

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
const NOOP = (fn: () => void) => fn()
1+
import { useEffect, useTransition } from 'react'
2+
import { devRenderIndicator } from './dev-render-indicator'
23

3-
/**
4-
* Returns a transition function that can be used to wrap router actions.
5-
* This allows us to tap into the transition state of the router as an
6-
* approximation of React render time.
7-
*/
84
export const useSyncDevRenderIndicator = () => {
9-
let syncDevRenderIndicator = NOOP
5+
const [isPending, startTransition] = useTransition()
106

11-
if (process.env.NODE_ENV === 'development') {
12-
const { useSyncDevRenderIndicatorInternal } =
13-
require('./use-sync-dev-render-indicator-internal') as typeof import('./use-sync-dev-render-indicator-internal')
7+
useEffect(() => {
8+
if (isPending) {
9+
devRenderIndicator.show()
10+
} else {
11+
devRenderIndicator.hide()
12+
}
13+
}, [isPending])
1414

15-
// eslint-disable-next-line react-hooks/rules-of-hooks
16-
syncDevRenderIndicator = useSyncDevRenderIndicatorInternal()
17-
}
18-
19-
return syncDevRenderIndicator
15+
return startTransition
2016
}

packages/next/src/client/components/use-reducer.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,38 @@ export function useUnwrapState(state: ReducerState): AppRouterState {
1717

1818
return state
1919
}
20-
2120
export function useReducer(
2221
actionQueue: AppRouterActionQueue
2322
): [ReducerState, Dispatch<ReducerActions>] {
2423
const [state, setState] = React.useState<ReducerState>(actionQueue.state)
25-
const actionDispatch = (action: ReducerActions) => {
26-
actionQueue.dispatch(action, setState)
27-
}
2824

29-
let syncDevRenderIndicator
25+
const syncDevRenderIndicatorInDevelopment =
26+
useSyncDevRenderIndicatorInDevelopment()
27+
28+
const dispatch = useCallback(
29+
(action: ReducerActions) => {
30+
syncDevRenderIndicatorInDevelopment(() =>
31+
actionQueue.dispatch(action, setState)
32+
)
33+
},
34+
[actionQueue, syncDevRenderIndicatorInDevelopment]
35+
)
36+
37+
return [state, dispatch]
38+
}
39+
40+
const PASS_THROUGH = (fn: () => void) => fn()
41+
42+
const useSyncDevRenderIndicatorInDevelopment = () => {
3043
if (process.env.NODE_ENV !== 'production') {
3144
const useSyncDevRenderIndicator =
3245
require('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator')
3346
.useSyncDevRenderIndicator as typeof import('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator').useSyncDevRenderIndicator
47+
3448
// eslint-disable-next-line react-hooks/rules-of-hooks
35-
syncDevRenderIndicator = useSyncDevRenderIndicator()
49+
const syncDevRenderIndicator = useSyncDevRenderIndicator()
50+
return syncDevRenderIndicator
51+
} else {
52+
return PASS_THROUGH
3653
}
37-
38-
const dispatchCallback =
39-
process.env.NODE_ENV !== 'production'
40-
? (action: ReducerActions) => {
41-
syncDevRenderIndicator!(() => actionDispatch(action))
42-
}
43-
: actionDispatch
44-
45-
const dispatch = useCallback(dispatchCallback, [
46-
actionQueue,
47-
dispatchCallback,
48-
syncDevRenderIndicator,
49-
])
50-
51-
return [state, dispatch]
5254
}

0 commit comments

Comments
 (0)