React-dates: Server Rendering Checksum Error

Created on 20 Feb 2017  路  4Comments  路  Source: airbnb/react-dates

When server-rendering we get a checksum error because DateInput is using isTouchDevice to set the readOnly attribute on the input. isTouchDevice is always rendering false on the server (since window isn't defined) regardless of whether the client device is actually a touchDevice. What's worse, this causes checksum errors even if the device is not a touch device anyway:

       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)  autocomplete="off" readonly="" aria-des
        (server)  autocomplete="off" aria-describedby=""

One way to fix this might be to move the device type check to run only after the component mounts and just use a constant value when on the server and on the initial render. Or, alternatively, you could allow the user to specify the value themselves if they so choose and thus figure out their own fix, such as guessing the device type on the server via the request's user agent header (similar to this discussion for media queries in react-responsive: https://github.com/contra/react-responsive/issues/43).

bug

Most helpful comment

I'm about to put one up :-) thanks tho!

All 4 comments

Indeed, moving the isTouchDevice check to componentDidMount is the proper solution here.

@ljharb Should I make a PR?

I'm about to put one up :-) thanks tho!

Fixed, will be in the next release.

Was this page helpful?
0 / 5 - 0 ratings