question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Two-pass rendering is broken in the hooks implementation

See original GitHub issue

See 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:

  1. server-side render
  2. first client-side pass (i.e. what would happen during hydration, before didMount/useEffect runs)
  3. 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:open
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
edorivaicommented, Oct 13, 2020

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.

1reaction
gpoolecommented, Jul 27, 2020

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 returning defaultMatches 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:

   // 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found