-
Notifications
You must be signed in to change notification settings - Fork 115
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
Two-pass rendering is broken in the hooks implementation #155
Comments
Other references: |
This could be a good time to get hook testing in place, too: https://react-hooks-testing-library.com/reference/api |
Minor tweak here: these expectations assume that |
I think the requirement is to force a second pass unless the client-side query result exactly matches I'd say for this hook if the query doesn't match Sketching it out, I think changing // 1st render pass returns default matches or default all-true match
const [matches, setMatches] = useState(() => {
// clientOnly option could force the 2nd pass off if it's safe to do so or we could invert this and require a flag to enable 2 pass behaviour
if (clientOnly) {
setUpMQLs();
return getMatches();
}
if (defaultMatches) {
return defaultMatches;
}
return Object.keys(queries).reduce(
(acc, key) => ({ ...acc, [key]: true }),
{}
);
});
useEffect(
() => {
// If clientOnly flag is set we don't need to update anything
if (!clientOnly) {
// Moved from useState callback
setUpMQLs();
// Force check and 2nd pass render with the actual media queries if anything changed
const clientMatches = getMatches();
// Check if the matches in the client have changed from the default value
// (Needs a shallowEqual implementation from somewhere :()
if (!shallowEqual(clientMatches, matches)) {
setMatches(clientMatches);
}
}
return () => activeQueries.current.forEach(({ mqListener }) => mqListener.cancel());
},
[],
);
// snip
return matches; 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. |
I'll implement it and see if this fixes it. Thank you! |
Any update on this one? 😃 |
@mrmartineau The blog did say it's okay to bump when...
So I just wanted to see... 🤷♂️ |
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. |
@edorivai no worries!! Just seeing! I too don't have the bandwidth as of now, so I totally understand 😄 -- Thanks for the upate! |
See comment here: #110 (comment)
In my head this points to a need for a test of two-pass rendering behavior, comparing the states of:
I'm pretty sure this is the set of expectations:
defaultMatches
is set, the first two would match and the third would notdefaultMatches
is not set, the first clientside pass should be completedefaultMatches
is set and we're only on the client, we should get two renders.Please correct me if the above is incorrect.
The text was updated successfully, but these errors were encountered: