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.

Breaks Server-Side Rendering

See original GitHub issue

Seems to affect both versions 5.x and 6.x. I have a decorator like this:

import { DragDropContext, DragLayer, DropTarget } from 'react-dnd'

export function DragAndDrop() {
  const context = DragDropContext(HTML5Backend)
  const layer = DragLayer((monitor) =>({ isDragging : monitor.isDragging() }))
  return component => context(layer(component))
}

The I decorate my app’s main component with @DragAndDrop(). This results in the .render() method returning null on server-side, and so server-side rendering returns emptyness. When I remove the @DragAndDrop() decorator everything renders as expected on server-side.

As a workaround, until the bug is fixed in this library, I modified my code this way:

import { DragDropContext, DragLayer, DropTarget } from 'react-dnd'

export function DragAndDrop() {
  if (typeof window === 'undefined') {
    return component => component
  }
  const context = DragDropContext(HTML5Backend)
  const layer = DragLayer((monitor) =>({ isDragging : monitor.isDragging() }))
  return component => context(layer(component))
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
catamphetaminecommented, Dec 4, 2018

Why is there no “Reopen” button? This is not fixed.

0reactions
catamphetaminecommented, Dec 5, 2018

@dkarbayev Actually, my workaround doesn’t work (I guess yours too). It still renders but then throws a warning in React.hydrate(): “Did not expect server HTML to contain …”. (doesn’t seem to throw the warning when using react-dnd@5 so I’m staying at version 5 until it’s fixed, if it ever gets fixed) It’s weird though because the server and client markups are identical. Maybe a React bug even.

@darthtrevino Here is a sample repo illustrating the bug. https://github.com/catamphetamine/react-dnd-bug-showcase

npm install && npm start http://localhost:3000 Disable JavaScript in DevTools and see “Text” on the page. Then go to src/pages/DragAndDrop.js and comment the lines:

	// A workaround to prevent `react-dnd` from breaking server-side rendering.
	// https://github.com/react-dnd/react-dnd/issues/1192
	if (typeof window === 'undefined') {
		return component => component
	}

Ctrl + C and npm start. See that nothing renders. The workaround is still not the solution because it breaks React.hydrate().

Read more comments on GitHub >

github_iconTop Results From Across the Web

[2.0.0] Server-side rendering breaks · Issue #1890 - GitHub
Description Reanimated v2 stable breaks server side rendering, because it uses window.performance in the constructor of JSReanimated.
Read more >
Stop Client Side and Server Side Rendering. Do This Instead.
Server -side rendering involves two renders. First, the server executes the data fetches and creates a fully rendered HTML page to send to...
Read more >
React Server Side Rendering Errors
Hi,. I am using MDB React Pro v4.8.7. My team uses SSR for all of our websites but I have been getting errors...
Read more >
Server Rendering with React and React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn how, why, and when to add server rendering to a React and React Router application.
Read more >
Server rendering - Material UI - MUI
The most common use case for server-side rendering is to handle the initial render when a user (or search engine crawler) first requests...
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