What actually changed in React 18
If you've just bumped react and react-dom to 18 and nothing seems different, that's expected. React 18 keeps working the old way until you change how the app is mounted.
If you’ve just bumped react and react-dom to 18 and nothing seems different, that’s expected. React 18 keeps working the old way until you change how the app is mounted. The console does tell you, though it’s easy to scroll past:
Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it’s running React 17.
So the old setup:
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));
becomes this:
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
Everything below only kicks in after that change.
Batching
This one you get for free. Before 18, React grouped several state updates into one render only inside its own event handlers, like onClick. Put an await or a setTimeout in between and each setter rendered on its own.
async function handleSave() {
setSaving(true);
await saveProfile(form);
setSaving(false); // React 17: one render here
setSavedAt(Date.now()); // and another one here
}
With createRoot, those last two lines give you a single render. The same goes for updates inside promises, timeouts and native event listeners. If you compare the Profiler before and after, you’ll usually see fewer commits, even if nobody would notice just by using the app.
There’s one case where this can bite. Say a websocket callback adds a message to state and then scrolls the list to the bottom on the very next line. In React 17, a setState outside a React event handler rendered right away, so the DOM was ready by that next line. In 18 it isn’t yet. If you really need the DOM updated immediately, flushSync does that:
import { flushSync } from 'react-dom';
flushSync(() => {
setMessages((list) => [...list, message]);
});
listRef.current.scrollTop = listRef.current.scrollHeight;
I’d keep flushSync for rare cases like this. Most of the time the cleaner fix is to do the scroll in an effect that runs when messages changes.
Transitions
This is the headline feature, and the one that takes a bit longer to click.
Until 18, once React started rendering an update, it had to finish it. Think of a search box that filters a few thousand rows. Every key press renders the whole list before the input can show the next letter, and typing feels stuck.
useTransition lets you tell React that an update isn’t urgent.
import { useState, useTransition } from 'react';
function Search({ items }) {
const [text, setText] = useState('');
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
function handleChange(e) {
setText(e.target.value);
startTransition(() => {
setQuery(e.target.value);
});
}
return (
<>
<input value={text} onChange={handleChange} />
{isPending && <p>Updating...</p>}
<Results items={items} query={query} />
</>
);
}
There are two pieces of state here. text is what the input shows, and it updates right away. query is what the list filters by, and because it’s set inside startTransition, React renders the list in the background. If another key press comes in before that render is done, React drops the unfinished work and starts again with the new value. isPending is true while this is going on, so you can show a small hint.
If the value comes in as a prop and you don’t own the setter, useDeferredValue does the same job:
const deferredQuery = useDeferredValue(query);
It’s easy to expect more from this than it gives. A transition doesn’t make the list render any faster. React can pause between components, not in the middle of one, so a single component that takes 300 ms still freezes the page for 300 ms. Transitions help most when the work is spread over many small components, like rows, and those rows still benefit from memo.
Other things you’ll run into
- Strict Mode now mounts components twice in development, so effects run, clean up and run again. That one deserves its own post.
- In TypeScript, React.FC no longer adds children to your props. Expect a few type errors, and add children: React.ReactNode where a component needs it.
- useId gives you an id that matches on the server and the client. It’s handy for tying a label to an input.
- Internet Explorer isn’t supported anymore.
If you’re upgrading, this order works well: bump the two packages, switch to createRoot, keep Strict Mode on and fix whatever it shows. Leave transitions until you actually have a slow screen.