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

Modification of useCurrentMatches() result triggers full re-render #516

Open
grobitto opened this issue Feb 26, 2025 · 0 comments
Open

Modification of useCurrentMatches() result triggers full re-render #516

grobitto opened this issue Feb 26, 2025 · 0 comments

Comments

@grobitto
Copy link

grobitto commented Feb 26, 2025

Describe the bug

Just had a very nasty bug which cost me a solid few hours to realise what is wrong.

I used useCurrentMatches() to get some routes metadata, but I needed deepest route data, so I used reverse() + find(), like this:

  createEffect(() => {
    const matches = useCurrentMatches();
    // should we show menu?
    const showMenu = matches().reverse().find((match) => match.route.info?.menu !== undefined);
    setVisible(showMenu ? showMenu.route.info?.menu : true);
  });

Everything was working fine, except I got full re-render of every page now. After a few hours of debugging and head-scratching I realised that the problem was that reverse() modified internal array and it messed up router state.

Your Example Website or App

see example above

Steps to Reproduce the Bug or Issue

  1. use useCurrentMatches();
  2. modify result array
  3. every page re-renders

Expected behavior

I think the best way is to make a copy of internal array and return it with useCurrentMatches() so it will not affect router internals. If it is not possible due to performance etc - this should be documented with a warning: do not modify result of this function call.

Screenshots or Videos

No response

Platform

  • OS: windows
  • Browser: chrome
  • Version: 0.15.3

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant