State Management

Managing context, state and hooks

Context API

We are using context for login methods - Auth0, JWT, Firebase.

src/contexts/configContext.jsx
....
// project import
import defaultConfig from 'config';
import useLocalStorage from 'hooks/useLocalStorage';
....

// ==============================|| CONFIG CONTEXT & PROVIDER ||============================== //

const ConfigContext = createContext(initialState);

function ConfigProvider({ children }) {

....
    const onChangeMenuOrientation = (menuOrientation) => {
        setConfig({
            ...config,
            menuOrientation
        });
    };

    const onChangeMiniDrawer = (miniDrawer) => {
        setConfig({
            ...config,
            miniDrawer
        });
    };

    const onChangeMode = (mode) => {
        setConfig({
            ...config,
            mode
        });
    };
....

 return (
        <ConfigContext.Provider
            value={{
                ...config,
                onChangeMenuOrientation,
                onChangeMiniDrawer,
                onChangeMode,
                onChangePresetColor,
                onChangeLocale,
                onChangeDirection,
                onChangeContainer,
                onChangeFontFamily,
                onChangeBorderRadius,
                onChangeOutlinedField,
                onReset
            }}
        >
            {children}
        </ConfigContext.Provider>
    );
}

ConfigProvider.propTypes = {
    children: PropTypes.node
};

export { ConfigProvider, ConfigContext };

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 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?