Skip to content

Commit

Permalink
misc: make useReducerWithDevtools noop on server (#41019)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
feedthejim committed Sep 29, 2022
1 parent e550222 commit 42713fb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/next/client/components/use-reducer-with-devtools.ts
Expand Up @@ -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,
initialState: ReturnType<typeof reducer>
): [
Expand Down Expand Up @@ -158,3 +171,8 @@ export function useReducerWithReduxDevtools(
}, [state])
return [state, dispatch, sync]
}

export const useReducerWithReduxDevtools =
typeof window !== 'undefined'
? useReducerWithReduxDevtoolsImpl
: useReducerWithReduxDevtoolsNoop

0 comments on commit 42713fb

Please sign in to comment.