Two-pass rendering is broken in the hooks implementation
See original GitHub issueSee comment here: https://github.com/ReactTraining/react-media/issues/110#issuecomment-647536404
In my head this points to a need for a test of two-pass rendering behavior, comparing the states of:
- server-side render
- first client-side pass (i.e. what would happen during hydration, before didMount/useEffect runs)
- second client-side pass
I’m pretty sure this is the set of expectations:
- if
defaultMatches
is set, the first two would match and the third would not - if
defaultMatches
is not set, the first clientside pass should be complete - if
defaultMatches
is set and we’re only on the client, we should get two renders.
Please correct me if the above is incorrect.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Hooks FAQ - React
Do Hooks replace render props and higher-order components? ... However, we recommend to split state into multiple state variables based on which values...
Read more >Understanding common frustrations with React Hooks
Learn about some of the drawbacks to using React Hooks. ... side effects and pass props down to these pure stateless component functions....
Read more >React.useEffect Hook – Common Problems and How to Fix ...
The reason our component is re-rendering is because our useEffect dependency is constantly changing. But why? We are always passing the same ...
Read more >Stop useEffect React Hook re-render multiple times with Async ...
Today I share a quick trick on how to stop unwanted responses from re- rendering a react component whose useEffect has an async...
Read more >Refactoring a render-prop-based component to use hooks!
Going to be refactoring https://github.com/kentcdodds/react-toggled to export a new fancy useToggle hook.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @jonmajorc, unfortunately no updates from my end. As pointed out in the sindresorhus article, I currently don’t have the bandwidth to actively take up issues here. I’m trying to keep an eye on the repo and I’ll try to help move PRs forward.
I think the requirement is to force a second pass unless the client-side query result exactly matches
defaultMatches
, even in the default case. The first pass in the two-pass render needs to match exactly what the server rendered, then the second pass can apply client-side changes.I’d say for this hook if the query doesn’t match
defaultMatches
on the client side, the hook should handle the two pass case by always returningdefaultMatches
first (so the first render after mounting matches exactly what SSR did) then update and return the actual matches after mounting (second render is the one that updates the client to match queries).Sketching it out, I think changing
useMedia
along these lines would work as I described:Not sure if that brings in any other issues. What do you think?
Edit: Noticed a bug with the clientOnly flag causing the queries to get stuck on default values after I posted, updated the code to fix that hopefully.