State Management
Managing context, state and hooks
Context API
We are using context for login methods - Auth0, JWT, Firebase.
import { createContext, ReactNode, useMemo } from 'react';
// project imports
import config from 'config';
import { useLocalStorage } from 'hooks/useLocalStorage';
// types
import { ConfigContextValue, ConfigStates } from 'types/config';
export type Props = {
children: ReactNode;
};
// ==============================|| CONFIG CONTEXT ||============================== //
export const ConfigContext = createContext<ConfigContextValue | undefined>(undefined);
// ==============================|| CONFIG PROVIDER ||============================== //
export function ConfigProvider({ children }: Props) {
const { state, setState, setField, resetState } = useLocalStorage<ConfigStates>('berry-config-vite-ts', config);
const memoizedValue = useMemo(() => ({ state, setState, setField, resetState }), [state, setField, setState, resetState]);
return <ConfigContext.Provider value={memoizedValue}>{children}</ConfigContext.Provider>;
}
State
Designing Actions
We designed the state structure based on the app's requirements.
Based on that list of things that can happen, we created a list of actions that our application will use:
Writing Reducers
Creating the Root Reducer - A Redux app really only has one reducer function: the "root reducer" function
Last updated
Was this helpful?