Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
React menu hamburger causes layout children to rerender

I am using React and Material UI v5. I have a menu component inside my layout that has children. When clicking on the menu it causes the children to refresh. I have tried solving it by wrapping the children components in React.memo(child) but the result is still the same. What am I doing wrong?

menu component

const MyMenu = ({ menuAnchorEl, setMenuAnchorEl }) => {
    const menuOpen = Boolean(menuAnchorEl);

    const handleMenuClose = () => {
        setMenuAnchorEl(null);
    };

    const menuOptions = [
        {
            link: '/overview',
            Icon: Dashboard,
            label: 'Overview',
        },
    ];

    const MenuOption = ({
        link,
        Icon,
        label,
    }: {
        link: string;
        Icon: OverridableComponent<SvgIconTypeMap<unknown, 'svg'>> & {
            muiName: string;
        };
        label: string;
    }) => {
        return (
            <NavLink href={link}>
                <MenuItem>
                    <ListItemIcon>
                        <Icon color="secondary" fontSize="small" />
                    </ListItemIcon>
                    <ListItemText sx={{ color: 'text.primary' }}>
                        {label}
                    </ListItemText>
                    <Typography variant="body2"></Typography>
                </MenuItem>
            </NavLink>
        );
    };

    return (
        <Menu
            anchorEl={menuAnchorEl}
            open={menuOpen}
            onClose={handleMenuClose}
            variant="selectedMenu"
        >
            <MenuList>
                {menuOptions.map((n) => (
                    <MenuOption key={n.link} {...n} />
                ))}
            </MenuList>
        </Menu>
    );
};

layout component

const MyLayout = observer(({ children }: IProps) => {
    const classes = useStyles();
    const [menuAnchorEl, setMenuAnchorEl] = React.useState<null | HTMLElement>(
        null,
    );

    return (
        <>
            <Hidden mdDown implementation="css">
                <InstallerMenu
                    menuAnchorEl={menuAnchorEl}
                    setMenuAnchorEl={setMenuAnchorEl}
                />
                <div className={classes.appFrame}>
                    <div className={classes.container}>{children}</div>
                </div>
            </Hidden>
        </>
    );
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have the MenuOption Component inside MyMenu and each time react re-render the main component MyMenu the MenuOption get re defined and cause all the children's to re-render again, in order to fix the issue you must take the MenuOption outside the main components and pass the required props to it, your code will look like this

alos read this article to know more why component inside component is bad

const MyMenu = ({ menuAnchorEl, setMenuAnchorEl }) => {
    const menuOpen = Boolean(menuAnchorEl);

    const handleMenuClose = () => {
        setMenuAnchorEl(null);
    };

    const menuOptions = [
        {
            link: '/overview',
            Icon: Dashboard,
            label: 'Overview',
        },
    ];

    return (
        <Menu
            anchorEl={menuAnchorEl}
            open={menuOpen}
            onClose={handleMenuClose}
            variant="selectedMenu"
        >
            <MenuList>
                {menuOptions.map((n) => (
                    <MenuOption key={n.link} {...n} />
                ))}
            </MenuList>
        </Menu>
    );
};


function MenuOption({requiredProps}){
return (
    <NavLink href={link}>
        <MenuItem>
            <ListItemIcon>
                <Icon color="secondary" fontSize="small" />
            </ListItemIcon>
            <ListItemText sx={{ color: 'text.primary' }}>
                {label}
            </ListItemText>
            <Typography variant="body2"></Typography>
        </MenuItem>
    </NavLink>
);
 };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!