Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate ReactDOM.render with async callback to createRoot()

Does anyone know what is the exact equivalent of this code for React 18, what happens with the async part?

ReactDOM.render(chart, container, async () => {
//code that makes some chart cells styling and adds cells to a worksheet via exceljs
})
like image 401
Bozhidar Petrov Avatar asked Oct 18 '25 00:10

Bozhidar Petrov


1 Answers

The render callback can be substituted with either the window.requestIdleCallback or window.setTimeout functions.

Before React 18

ReactDOM.render(<App />, rootElement, callback);

React >=18

ReactDOM.createRoot(rootElement).render(<App />);
requestIdleCallback(callback);

or:

ReactDOM.createRoot(rootElement).render(<App />);
setTimeout(callback, 0);
like image 53
dedoussis Avatar answered Oct 19 '25 14:10

dedoussis