score:0

Accepted answer
import { useSelector } from 'react-redux';

const checkAccess = (access, role) => {
  if (typeof access === 'string') {
    return role === access;
  };
  if(Array.isArray(access)){
   return access.includes(role);
 }

 return access;
};

const Menu = ({ items, isCollapseMenu }) => {
  const classes = useStyles();
  const role = useSelector((state) => state.role);

  return (
    <div className={classes.root}>
      {items.filter(itemToFilter => checkAccess(itemToFilter.userAccess, role)).map(
        (item) => (
            <MenuItem
              key={item.id}
              title={item.title}
              icon={item.icon}
              text={item.text}
              shortText={item.shortText}
              url={item.url}
              submenu={item.submenu}
              isCollapseMenu={isCollapseMenu}
            />
          )
      )}
    </div>
  );
};

score:0

const checkAccess = (access, role) => {
  if (typeof access === 'string') {
    return role.indexOf(access) > -1;
  };
  return access;
};

score:0

const checkAccess = (access, role) => {
 if (typeof access === 'string') { // String check
    return role === access;
 };

 if(Array.isArray(access)){ //Array check
   return access.includes(role);
 }

 return access; // Boolean check
};

Related Query

More Query from same tag