Skip to content

[Slider] React.StrictMode fixes #794

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

Merged
merged 3 commits into from
Jul 23, 2021
Merged

[Slider] React.StrictMode fixes #794

merged 3 commits into from
Jul 23, 2021

Conversation

jjenzz
Copy link
Contributor

@jjenzz jjenzz commented Jul 22, 2021


NOTE: PR will be easier to review with hidden whitespace changes


Fixes #615
Fixes #773

Replaces our old collection logic with our new Collection. This fixes the React.StrictMode issues that have been reported lately, but means the thumbs will no longer be in the correct position on initial render for SSR as our new Collection component doesn't handle this case.

More information here. Thanks to @andy-hook for narrowing down collection code as the issue.

It seems this is a less jarring experience for now so I'm thinking we can look into improving SSR experience across our components separately.

Tested in:

  • NextJS React.StrictMode
  • Create React App React.StrictMode
  • Snowpack React.StrictMode
CleanShot.2021-07-22.at.13.18.18.mp4

@jjenzz jjenzz force-pushed the fix-slider-strict-mode branch 2 times, most recently from a6103a0 to ef7ec91 Compare July 22, 2021 15:54
@jjenzz jjenzz force-pushed the fix-slider-strict-mode branch from ef7ec91 to 28deb92 Compare July 22, 2021 15:57
@jjenzz jjenzz merged commit 825d002 into main Jul 23, 2021
@jjenzz jjenzz deleted the fix-slider-strict-mode branch July 23, 2021 08:31
Copy link
Contributor

@benoitgrelard benoitgrelard left a comment

Choose a reason for hiding this comment

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

Nice work on this! 👍

* render them in the wrong position before they snap into the correct position during
* hydration which would be visually jarring.
*/
return isInitialRenderRef.current || index !== -1 ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're checking for the initial render case here, is the index !== -1 just a safety to not render the whole thing if for some reason that thumb wasn't tracked? I can't think of any reason why this check would be necessary right now.

Copy link
Contributor Author

@jjenzz jjenzz Jul 26, 2021

Choose a reason for hiding this comment

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

It's necessary for this case:

<Slider.Root value={[20]}>
	...
	<Slider.Thumb />
	<Slider.Thumb />
</Slider.Root>

Notice how there are two thumbs but only one value? The second thumb won't render. We handled this case before with the value !== undefined check but perhaps it's okay to let them render a thumb that does nothing and expect them to remove the redundant JSX instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that’s probably fine too seeing as it’s just a user error that they would figure out pretty easily.

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 can create a PR for that then 👍

Comment on lines +512 to +514
* We hide thumbs on the first render while we work out the index, otherwise SSR will
* render them in the wrong position before they snap into the correct position during
* hydration which would be visually jarring.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might have been ok to leave it as is rather than doing more work specifically to hide it?
Are you thinking that we'll deal with this anyway differently at some point?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You think it's okay for them to render in the wrong position and snap into a different position during hydration?

The plan is to fix the SSR issue eventually so that it renders in the correct position server-side.

Copy link
Contributor

Choose a reason for hiding this comment

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

It’s not great but I wonder how visible it is in a real use case. But yeah regardless SSR is something we need to look at some point. We might end up with some controlled overrides (like providing the index yourself) when you want to make sure it’s ok on first server render.

Copy link
Contributor Author

@jjenzz jjenzz Jul 26, 2021

Choose a reason for hiding this comment

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

We might end up with some controlled overrides (like providing the index yourself)

I was thinking this too since we already have the index prop on the Impl part. We just don't expose it via the SliderThumb part atm.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think these kind of solutions would definitely be the easiest. Simple trade offs.

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.

[Slider] Not working in Next.js when reactStrictMode is enabled [Slider] Thumb loses focus when value changes
3 participants