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.

[v2] "React attempted to reuse markup error" on Isomorphic SSR app and Webpack HMR

See original GitHub issue

Version

2.0.0-17

Reproduction

Steps to reproduce

git clone https://github.com/sysgears/apollo-fullstack-starter-kit yarn yarn seed yarn watch Open http://localhost:3000 in browser (this is webpack dev server URL).

Change src/client/app/app.jsx in any way. For example add line console.log("sc comp id:", Footer.styledComponentId); Now refresh the page in browser (this will force page rendering server-side, you can check what page is rendered on server by opening http://localhost:8080 and checking the page)

Expected Behavior

No errors in Web Browser console

Actual Behavior

React error in Web Browser console: Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server: (client) ><footer class="sc-bdVaJa bRzCCq" data-r (server) ><footer class="sc-bxivhb gCGYyZ" data-r

What is happening is the styled component id changes each time app.jsx is changed, due to these lines: https://github.com/styled-components/styled-components/blob/b9778f10f23ce7260ce99c72e7729e1b539521c4/src/models/StyledComponent.js#L30-L33

Each time app.jsx module is hot reloaded identifiers will increase counter. And when you force page reload in web browser, on web browser side identifiers will be empty immediately after page refresh. But on the server side identifiers will not be cleared and continue to increase counter on each module change that uses styled-components.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
larixercommented, May 5, 2017

@philpl Yes, I believe it should be consistent if hot module reloading is on… I confirm though, that using https://github.com/styled-components/babel-plugin-styled-components with: [“styled-components”, { “ssr”: true } ] make the ids consistent and works good with Webpack HMR, the React error about mismatched markup doesn’t show up

0reactions
kittencommented, May 22, 2017

@ansumanshah @bhough This is due to the styledComponentId being used for generating the “base” classname. When only displayName is set, it is used to generate a styledComponentId and is thus human-readable. When ssr is set, a styledComponentId is already present.

The displayName option is actually literally only for the displayName but has the side effect of generating a human-readable classname.

If you value this classname to be readable, I suggest to only turn on ssr in production 😄

Edit: I guess the babel plugin’s docs will have to be improved so that it explains this better. Might become part of the styled-components-website.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: React attempted to reuse markup in a container but ...
js, Express, Webpack, React app working. I'm getting the following error. Any suggestions on how to fix it? Warning: React attempted to reuse...
Read more >
React: Server-side rendering and hot reloading | by Justin Jung
Solution: Webpack HMR is good enough.​​ A root component contains the entire app in it, so change in any component will be reflected...
Read more >
Warning: React attempted to reuse markup in a container but ...
I came across this issue on an Isomorphic app I was working on. What worked for me is, believe it or not, Emptying...
Read more >
Complete Intro to React
This Course Has Been Updated. The React Learning Path has the latest version of the course. The code, website and materials for the...
Read more >
Intro to React Server Side Rendering | by Suhan Wijaya
We will use React, webpack, and Express to build an SSR app that ... Server receives HTTP request and turns React JSX into...
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