Unverified Commit 42713fbf authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

misc: make useReducerWithDevtools noop on server (#41019)



Making this function a noop on server for bundle size gains with DCE since most of the code is in an useEffect and is not gonna run

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent e550222d
...@@ -104,7 +104,20 @@ function devToolReducer( ...@@ -104,7 +104,20 @@ function devToolReducer(
} }
} }
export function useReducerWithReduxDevtools( function useReducerWithReduxDevtoolsNoop(
fn: typeof reducer,
initialState: ReturnType<typeof reducer>
): [
ReturnType<typeof reducer>,
Dispatch<ReducerAction<typeof reducer>>,
() => void
] {
const [state, dispatch] = useReducer(fn, initialState)
return [state, dispatch, () => {}]
}
function useReducerWithReduxDevtoolsImpl(
fn: typeof reducer, fn: typeof reducer,
initialState: ReturnType<typeof reducer> initialState: ReturnType<typeof reducer>
): [ ): [
...@@ -158,3 +171,8 @@ export function useReducerWithReduxDevtools( ...@@ -158,3 +171,8 @@ export function useReducerWithReduxDevtools(
}, [state]) }, [state])
return [state, dispatch, sync] return [state, dispatch, sync]
} }
export const useReducerWithReduxDevtools =
typeof window !== 'undefined'
? useReducerWithReduxDevtoolsImpl
: useReducerWithReduxDevtoolsNoop
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment