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

Cache locale dict lest child hooks always re-render #6185

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

IgnusG
Copy link

@IgnusG IgnusG commented Apr 11, 2024

Closes #6184

The child hooks like useCalendarGrid etc. use useLocale to fetch their locale data.

As such whenever this context value changes these hooks (and the component they are part of are forced to re-render) even if nothing else is changed. Even if the locale doesn't change as long as I18nProvider renders (eg. as part of a parent component update) it creates a new dict which forces a re-render.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Perform the steps in #6184 and observe the rendering of I18nProvider when locale doesn't change no longer causes renders inside of components using the calendar hooks.

@IgnusG IgnusG closed this Apr 11, 2024
@IgnusG IgnusG reopened this Apr 11, 2024
snowystinger
snowystinger previously approved these changes Apr 12, 2024
Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

thanks the pr

locale,
direction: isRTL(locale) ? 'rtl' : 'ltr'
};
}, [locale);
Copy link
Author

@IgnusG IgnusG Apr 12, 2024

Choose a reason for hiding this comment

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

Forgot to include defaultLocale in the dependency array here too.

It has event listeners attached to it so it's possible it changes. If it does we must refresh the memoized value too.

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

Successfully merging this pull request may close these issues.

I18nProvider re-renders unnecessarily due to a new dict value being re-created every time
2 participants