From 11b6c87a5181e41a8e5597ca1eeb949426658493 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Wed, 3 Jan 2024 07:36:54 +0100 Subject: [PATCH] Router: Use a single RouterContext (#9792) --- packages/router/src/router-context.tsx | 36 +++++++------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/packages/router/src/router-context.tsx b/packages/router/src/router-context.tsx index 5fec55ce5752..e5d34c418cf5 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' @@ -27,46 +27,28 @@ 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 children: React.ReactNode } -function stateReducer(state: RouterState, newState: Partial) { - return { ...state, ...newState } -} - export const RouterContextProvider: React.FC = ({ useAuth, paramTypes, children, }) => { - const [state, setState] = useReducer(stateReducer, { - useAuth: useAuth || useNoAuth, - paramTypes, - }) + const state = useMemo( + () => ({ + useAuth: useAuth || useNoAuth, + paramTypes, + }), + [useAuth, paramTypes] + ) return ( - - {children} - + {children} ) }