Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a warning in console "You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before."."

I am trying to implement a Redux in a simple React application that uses the latest version (React 18). Here is my code in index.js so far:

import React from 'react';
import ReactDOM from 'react-dom/client';
import { store, App } from './App';
import { createStore } from 'redux'

export const renderApp = () => {
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
}

renderApp()

store.subscribe(renderApp)

However, I get a warning in the console that says:

"You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it"

What is the supposed to mean, and how would I need to change my code in index.js?

like image 519
WannaInternet Avatar asked Oct 29 '25 07:10

WannaInternet


1 Answers

The most basic implementation would be something like the following, which creates a single root, renders against it, and then passes a callback which closes over that root for further subscription calls.

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

store.subscribe(() => root.render(<App />));
like image 196
pilchard Avatar answered Oct 31 '25 22:10

pilchard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!