Render Menu from the backend
Render menu via backend
Berry is already rendering the menu from the backend. The widget menus (Statistics, Data, and Chart) are rendered via the back end.
You can check the Fack backend API here that we used to render the menu: https://github.com/phoenixcoded20/mock-data-api-nextjs
To add a menu from the backend, you can follow the steps below:
- Open the file menu.ts - (src/api/menu.ts), and edit the API URL in the useGetMenu function.
...
import { fetcher } from 'utils/axios';
// types
import { MenuProps } from 'types/menu';
import { NavItemType } from 'types';
export const endpoints = {
  key: 'api/menu',
  
  widget: '/widget' // server URL
};
....
export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.widget, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });
  const memoizedValue = useMemo(
    () => ({
      menu: data?.widget as NavItemType,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );
  return memoizedValue;
}
....- Create Menu File - Add file menu.tsx in - src/menu-items, and copy code from- widget.tsx- (src/menu-items/). Set icons and local files according to the API response.
 
- Open the file index - (- src/layout/MainLayout/MenuList/index.tsx- ), and add the code below the line.
import { useLayoutEffect, useState } from 'react'
...
import menuItem from 'menu-items';
import { Menu } from 'menu-items/widget';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...
// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //
export default function Navigation() {
...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState<{ items: NavItemType[] }>({ items: [] });
let widgetMenu = Menu();
useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-widget') {
        return true;
      }
      return false;
    });
    if (menuLoading) {
      menuItem.items.splice(0, 0, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && widgetMenu?.id !== undefined && !isFound) {
      menuItem.items.splice(0, 1, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading]);Berry is already rendering the menu from the backend. The widget menus (statistics, Data, Chart) are rendered via the back end.
You can check the Fack backend API here that we used to render the menu: https://github.com/phoenixcoded20/mock-data-api-nextjs
To add a menu from the backend, you can follow the steps below:
- Open the file menu.ts - (src/api/menu.js), and edit the API URL in the useGetMenu function.
...
import { fetcher } from 'utils/axios';
export const endpoints = {
  key: 'api/menu',
  widget: '/widget' // server URL
};
....
export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.widget, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });
  const memoizedValue = useMemo(
    () => ({
      menu: data?.widget,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );
  return memoizedValue;
}
....- Create Menu File - Add file menu.jsx in - src/menu-items, and copy code from- widget.jsx- (src/menu-items/). Set icons and local files according to API response.
 
- Open the file index - (- src/layout/MainLayout/MenuList/index.jsx- ), and add the below code of the line.
import { useLayoutEffect, useState } from 'react'
...
import menuItem from 'menu-items';
import { Menu } from 'menu-items/widget';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...
// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //
export default function Navigation() {
...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState<{ items }>({ items: [] });
let widgetMenu = Menu();
useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-widget') {
        return true;
      }
      return false;
    });
    if (menuLoading) {
      menuItem.items.splice(0, 0, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && widgetMenu?.id !== undefined && !isFound) {
      menuItem.items.splice(0, 1, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading]);Berry is already rendering the menu from the backend. The widget menus (statistics, Data, Chart) are rendered via the back end.
You can check the Fack backend API here that we used to render the menu: https://github.com/phoenixcoded20/mock-data-api-nextjs
To add a menu from the backend, you can follow the steps below:
- Open the file menu.ts - (src/api/menu.ts), and edit the API URL in the useGetMenu function.
...
import { fetcher } from 'utils/axios';
// types
import { MenuProps } from 'types/menu';
import { NavItemType } from 'types';
export const endpoints = {
  key: 'api/menu',
  widget: '/widget' // server URL
};
....
export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.widget, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });
  const memoizedValue = useMemo(
    () => ({
      menu: data?.widget as NavItemType,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );
  return memoizedValue;
}
....- Create Menu File - Add file menu.tsx in - src/menu-items, and copy code from- widget.tsx- (src/menu-items/). Set icons and local files according to API response.
 
- Open the file index - (- src/layout/MainLayout/MenuList/index.tsx- ), and add the below code of the line.
import { useLayoutEffect, useState } from 'react'
...
import menuItem from 'menu-items';
import { Menu } from 'menu-items/widget';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...
// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //
export default function Navigation() {
...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState<{ items: NavItemType[] }>({ items: [] });
let widgetMenu = Menu();
useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-widget') {
        return true;
      }
      return false;
    });
    if (menuLoading) {
      menuItem.items.splice(0, 0, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && widgetMenu?.id !== undefined && !isFound) {
      menuItem.items.splice(0, 1, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading])Berry is already rendering the menu from the backend. The widget menus (statistics, Data, Chart) are rendered via the back end.
You can check the Fack backend API here that we used to render the menu: https://github.com/phoenixcoded20/mock-data-api-nextjs
To add a menu from the backend, you can follow the steps below:
- Open the file menu.ts - (src/api/menu.js), and edit the API URL in the useGetMenu function.
...
import { fetcher } from 'utils/axios';
export const endpoints = {
  key: 'api/menu',
  widget: '/widget' // server URL
};
....
export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.widget, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });
  const memoizedValue = useMemo(
    () => ({
      menu: data?.widget,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );
  return memoizedValue;
}
....- Create Menu File - Add file menu.jsx in - src/menu-items, and copy code from- widget.jsx- (src/menu-items/). Set icons and local files according to API response.
 
- Open the file index - (- src/layout/MainLayout/MenuList/index.jsx- ), and add the below code of the line.
import { useLayoutEffect, useState } from 'react'
...
import menuItem from 'menu-items';
import { Menu } from 'menu-items/widget';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...
// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //
export default function Navigation() {
...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState<{ items }>({ items: [] });
let widgetMenu = Menu();
useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-widget') {
        return true;
      }
      return false;
    });
    if (menuLoading) {
      menuItem.items.splice(0, 0, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && widgetMenu?.id !== undefined && !isFound) {
      menuItem.items.splice(0, 1, widgetMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading])Last updated
Was this helpful?