Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React warning render()

why this happened? Warning: render(): Target node has markup rendered by React, but there are unrelated nodes as well. This is most commonly caused by white-space inserted around server-rendered markup.

<section id="container-wrapper">
    <div data-reactid=".0.1.0.1">
        loading.....
    </div>
</section>

// App.js

ReactDOM.render(<App routes={routes} /> , document.getElementById('container-wrapper'));
like image 370
zhoushx3 Avatar asked Dec 03 '15 08:12

zhoushx3


People also ask

Is ReactDOM render still used?

The render() method of the react-dom package is considered legacy starting react-dom version 18. The method is replaced with the createRoot() method that is exported from react-dom/client . The createRoot() method takes the root element as a parameter and creates a React root.

How do you fix warning ReactDOM render is no longer supported in React 18 use createRoot instead?

To solve the error, create a root element and use the ReactDOMClient. render method instead. import {StrictMode} from 'react'; import {createRoot} from 'react-dom/client'; import App from './App'; // 👇️ IMPORTANT: use correct ID of your root element // this is the ID of the div in your index.

Can I ignore React warnings?

The Yellow warning box in React Native can be both helpful and annoying. There are many errors that can be ignored but end up blocking necessary pieces of the application. To disable the yellow box place console. disableYellowBox = true; anywhere in your application.

How do I stop Rerendering in React?

1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With this technique, developers can avoid unnecessary renderings and reduce the computational load in applications.


1 Answers

I ran into this issue rendering markup server-side. If you have something like this (ejs example):

<div id="app">
    <%- markup %>
</div>

Try removing all surrounding white space. The following got rid of the warning in my case.

<div id="app"><%- markup %></div>
like image 114
aw04 Avatar answered Oct 23 '22 09:10

aw04