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.

[SSR] Rehydrating app contains the wrong classnames.

See original GitHub issue

I use the match render prop callback to set a prop which itself toggles a className. This renders fine on the server but on first load the client does not re-render to correctly display the className. I have even tried to for a state update using setState in an onChange callback provided to react-responsive. For some reason the matches values is correct when I examine the react tree but the DOM node is missing the correct class name. If I force the breakpoint by shrinking the window and expanding it again everything works correctly. I need that first render to be correct. The react-responsive-redux solution is not good for this use case as it tries to guess the viewport based on the user agent. I don’t need that, I just need the responsive component to re-render in the client. Is there something I am missing to accomplish this? Here is the component I’m trying to get to work:

import React from 'react';
import Responsive from 'react-responsive';
import BorderedRow from '../../../components/BorderedRow';
import styles from './index.scss';

const Posts = ({ posts, className }) => (
  <Responsive minWidth={981}>
    {match => (
      <div>
        {posts?.map?.(
          ({ id }) => (
            <BorderedRow stacked={match} key={id} className={styles.post}>
              <span>the rest of the content</span>
            </BorderedRow>
          )
        )}
      </div>
    )}
  </Responsive>
);

export default Posts;

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:13
  • Comments:33 (8 by maintainers)

github_iconTop GitHub Comments

43reactions
jnlsncommented, Feb 27, 2020

the solution @nhuanhoangduc proposed works quite well. I modified it a bit to use hooks instead of hocs 🙂

import { useState, useLayoutEffect } from 'react';
import { useMediaQuery } from 'react-responsive';
import Theme from '../components/Theme';

const { breakpoint: breakpointMobile, breakpointTablet } = Theme;

function useResponsive() {
  const [isClient, setIsClient] = useState(false);

  const isMobile = useMediaQuery({
    maxWidth: breakpointMobile,
  });

  const isTablet = useMediaQuery({
    minWidth: breakpointMobile,
    maxWidth: breakpointTablet,
  });

  const isDesktop = useMediaQuery({
    minWidth: breakpointTablet,
  });

  useLayoutEffect(() => {
    if (typeof window !== 'undefined') setIsClient(true);
  }, []);

  return {
    isDesktop: isClient ? isDesktop : true,
    isTablet: isClient ? isTablet : false,
    isMobile: isClient ? isMobile : false,
  };
}

export default useResponsive;
7reactions
team-gguyscommented, Mar 14, 2020

@isotopeee I recommend using class component to understand deeply react lifecycle before moving to functional component and hook.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[SSR] Rehydrating app contains the wrong classnames. #162
I use the match render prop callback to set a prop which itself toggles a className. This renders fine on the server but...
Read more >
The Perils of Rehydration - Josh W Comeau
My React component was rendering in the wrong spot! ... When a React app rehydrates, it assumes that the DOM structure will match....
Read more >
Handling the React server hydration mismatch error
Typical fix​​ The problem is that the server-side render doesn't contain the final className that is generated on the client. Most CSS-in-JS ...
Read more >
Tag: JavaScript - somewhat abstract
Over the course of the last ten posts, we have created a simple React app that ... Patch a console with new error...
Read more >
wrong class names on pages with different content rendered ...
From the ReactDOM.hydrate documentation: React expects that the rendered content is identical between the server and the client.
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