Skip to content
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

Error while using randomness for inline styling #1173

Closed
manuelro opened this issue Feb 16, 2017 · 4 comments
Closed

Error while using randomness for inline styling #1173

manuelro opened this issue Feb 16, 2017 · 4 comments

Comments

@manuelro
Copy link

There is some weird error warning about issues with server and client sides rendering not being the same. The issue shows up only if I use rgb colors in CSS-in-JS for inline styling.

The error:

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) ackground-color:rgb(232, 232, 49);" data
 (server) ackground-color:rgb(49, 232, 140);" data

If I replace the color from rgb to any named-color ('blue', 'red', etc.), the warning disappears.

The code

    let itemStyles = {  backgroundColor: backgroundColor  }
    return <li className="nav-list__item" style={ itemStyles }>...</li>

And I think the differences between the client color and the server color has to do with randomness. My script is generating a random color based on hsl and translated to rgb, this is how this is intended to be. Is there any chance the random number generated on the server is being overridden by the random number generated on the client? If so, is this the intended behaviour? I know isomorphic JS is something hard to deal with. Any advice would be greatly appreciated.

@manuelro manuelro changed the title Error while randomness for inline styling Error while using randomness for inline styling Feb 16, 2017
@eezing
Copy link

eezing commented Feb 16, 2017

@manuelro The warning is expected behavior. Yes the different random RGB values from server to client are causing the issue. If you're interested, you can learn about universal react here.

@Ehesp
Copy link
Contributor

Ehesp commented Feb 16, 2017

Try setting the colour outside of the components for the first render, and only generating if it doesn't exist (on the server).

@manuelro
Copy link
Author

manuelro commented Feb 16, 2017

Thanks for the advice. I'll give it a try.

@timneutkens
Copy link
Member

Closing as this is not an issue produced by Next.js, feel free to re-open if needed ❤️

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants