Performance: Creating first React app
This affects the initial page load speed and time to interactive by controlling how React and its dependencies load and render the app.
import React from 'react'; import { createRoot } from 'react-dom/client'; function App() { return <h1>Hello, React!</h1>; } const container = document.getElementById('root'); const root = createRoot(container); root.render(<App />);
import React from 'react'; import ReactDOM from 'react-dom'; function App() { return <h1>Hello, React!</h1>; } ReactDOM.render(<App />, document.getElementById('root'));
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| ReactDOM.render (legacy) | Single root node | 1 reflow | Low paint cost | [!] OK |
| createRoot.render (modern) | Single root node | 1 reflow | Low paint cost | [OK] Good |
ReactDOM.createRoot method in a React app?App?import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return Welcome to React!
;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return <h1>Hello World</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(App);div with a class container. Which code correctly creates and renders this app?className instead of class for CSS classes.root.render(<App />) to render the component, not just the function name.Pass the test and this pattern moves unknown