# State Management

## Context API

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

We are using context for login methods - **`Auth0, JWT, Firebase, AWS, Supabase.`**

## SWR

SWR is a popular library used for data fetching and state management in React applications. Data fetching is covered in the [Axios API calls](https://codedthemes.gitbook.io/Mantis/v4.0.0/axios-api-calls) section. Here, we are going to cover state management using SWR.

Mantis is managing the state of following using SWR

1. Snackbar states
2. Menu states&#x20;
3. E-commerce Cart&#x20;

### 1. Snackbar&#x20;

Snackbar is used to show a notification in an application. Whenever a component dispatches an action with data to show a snackbar, it appears in the app. The initial state of snackbar is being captured using useSWR hooks, and after that, we are mutating its state based on action calls. Following is the initial state:

{% code title="src/api/snackbar.ts" %}

```typescript
const initialState: SnackbarProps = {
  action: false,
  open: false,
  message: 'Note archived',
  anchorOrigin: {
    vertical: 'bottom',
    horizontal: 'right'
  },
  variant: 'default',
  alert: {
    color: 'primary',
    variant: 'filled'
  },
  transition: 'Fade',
  close: false,
  actionButton: false,
  maxStack: 3,
  dense: false,
  iconVariant: 'usedefault',
  hideIconVariant: false
};
```

{% endcode %}

These values have been initialized using useSWR like below:

{% code title="src/api/snackbar.ts" %}

```typescript
export function useGetSnackbar() {
  const { data } = useSWR(endpoints.key, () => initialState, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });

  const memoizedValue = useMemo(() => ({ snackbar: data! }), [data]);

  return memoizedValue;
}
```

{% endcode %}

Now, how to read these values and update as well.&#x20;

To read the values and listen, following the SWR hooks is sufficient in the component. Whenever any changes happen, it is activated like a redux selector and acts based on logic.

{% code title="@extended\Snackbar.tsx" %}

```typescript
...
import { closeSnackbar, useGetSnackbar } from 'api/snackbar';
...

// ==============================|| SNACKBAR ||============================== //

const Snackbar = () => {
  const { snackbar } = useGetSnackbar();
  ...
  ...
};

export default Snackbar;

```

{% endcode %}

To open the Snack bar, we need to change the value and mutate the state. Here is how it is done

{% code title="src/api/snackbar.ts" %}

```typescript
export function openSnackbar(snackbar: SnackbarProps) {
  // to update local state based on key

  const { action, open, message, anchorOrigin, variant, alert, transition, close, actionButton } = snackbar;

  mutate(
    endpoints.key,
    (currentSnackbar: any) => {
      return {
        ...currentSnackbar,
        action: action || initialState.action,
        open: open || initialState.open,
        message: message || initialState.message,
        anchorOrigin: anchorOrigin || initialState.anchorOrigin,
        variant: variant || initialState.variant,
        alert: {
          color: alert?.color || initialState.alert.color,
          variant: alert?.variant || initialState.alert.variant
        },
        transition: transition || initialState.transition,
        close: close || initialState.close,
        actionButton: actionButton || initialState.actionButton
      };
    },
    false
  );
}
```

{% endcode %}

And then the method *openSnackbar* will be called from components to open snackbar.

{% code title="apps\invoice\create.tsx" %}

```typescript
...
import { openSnackbar } from 'api/snackbar';
// types
import { SnackbarProps } from 'types/snackbar';
...

// ==============================|| INVOICE - CREATE ||============================== //

const Create = () => {
  ...
  const handlerCreate = (values: any) => {
    const newList: InvoiceList = {
      ...
    };
    ...
    openSnackbar({
      open: true,
      message: 'Invoice Added successfully',
      anchorOrigin: { vertical: 'top', horizontal: 'right' },
      variant: 'alert',
      alert: {
        color: 'success'
      }
    } as SnackbarProps);
    ...
  };



  return (
    <MainCard>
      ...
    </MainCard>
  );
};

export default Create;

```

{% endcode %}
