diff --git a/packages/router/src/router-context.tsx b/packages/router/src/router-context.tsx index 74a421773e98..4c753e2425b8 100644 --- a/packages/router/src/router-context.tsx +++ b/packages/router/src/router-context.tsx @@ -1,4 +1,4 @@ -import React, { useReducer, createContext, useContext } from 'react' +import React, { createContext, useContext, useMemo } from 'react' import type { AuthContextInterface } from '@redwoodjs/auth' import { useNoAuth } from '@redwoodjs/auth' @@ -29,19 +29,6 @@ export interface RouterState { const RouterStateContext = createContext(undefined) -export interface RouterSetContextProps { - setState: (newState: Partial) => void -} - -const RouterSetContext = createContext< - React.Dispatch> | undefined ->(undefined) - -/** - * This file splits the context into getter and setter contexts. - * This was originally meant to optimize the number of redraws - * See https://kentcdodds.com/blog/how-to-optimize-your-context-value - */ export interface RouterContextProviderProps extends Omit { useAuth?: UseAuth @@ -50,10 +37,6 @@ export interface RouterContextProviderProps children: React.ReactNode } -function stateReducer(state: RouterState, newState: Partial) { - return { ...state, ...newState } -} - export const RouterContextProvider: React.FC = ({ useAuth, paramTypes, @@ -61,18 +44,19 @@ export const RouterContextProvider: React.FC = ({ activeRouteName, children, }) => { - const [state, setState] = useReducer(stateReducer, { - useAuth: useAuth || useNoAuth, - paramTypes, - routes, - activeRouteName, - }) + const state = useMemo( + () => ({ + useAuth: useAuth || useNoAuth, + paramTypes, + routes, + activeRouteName, + }), + [useAuth, paramTypes, routes, activeRouteName] + ) return ( - - {children} - + {children} ) }