Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: useMemo in menus #1253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/common/components/PageLayout.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import {
AppBar,
Breadcrumbs,
Expand Down Expand Up @@ -74,6 +74,8 @@ const PageLayout = ({ menu, breadcrumbs, children }) => {

const [openDrawer, setOpenDrawer] = useState(false);

const menuMemo = useMemo(() => menu, []);

return desktop ? (
<div className={classes.desktopRoot}>
<Drawer
Expand All @@ -88,7 +90,7 @@ const PageLayout = ({ menu, breadcrumbs, children }) => {
<PageTitle breadcrumbs={breadcrumbs} />
</Toolbar>
<Divider />
{menu}
{menuMemo}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the same thing be done in mobile layout?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how does it actually prevent re-rendering? Because the menu is still passed as a parameter every time. Have you verified that it actually works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not render again because in this case the useMemo has an empty dependency [ ].
I share the video of the tests.

Video

2024-07-02.00-48-07.mp4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The menu won't be re-rendered, but the page will, no?

I'm not sure I understand what I'm supposed to see on the video, but I see re-rendering around 4th second.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the page is rendered by the websocket because I have GPS connected to the traccar server.
The socket updates the state of the store, and this generates the render. Don't worry, it's part of React.
Version 19 is close and will bring performance improvements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I don't understand what I'm supposed to see in the video.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously all items in the report navigation menu were rendered with each socket update. With useMemo it no longer happens.

Copy link
Member

@tananaev tananaev Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the problem was with re-rendering the map, not the menu. Why would re-rendering the menu a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that was what you were referring to in the comment #1251 (comment)

I agree with you, menu rendering is not a problem.

Also thank you for your time, because these days I am understanding your platform better.

</Drawer>
<div className={classes.content}>{children}</div>
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/settings/components/EditItemView.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import {
Container, Button, Accordion, AccordionDetails, AccordionSummary, Skeleton, Typography, TextField,
Expand Down Expand Up @@ -54,8 +54,10 @@ const EditItemView = ({
}
});

const menuMemo = useMemo(() => menu, []);

return (
<PageLayout menu={menu} breadcrumbs={breadcrumbs}>
<PageLayout menu={menuMemo} breadcrumbs={breadcrumbs}>
<Container maxWidth="xs" className={classes.container}>
{item ? children : (
<Accordion defaultExpanded>
Expand Down