Understanding Error Messages
React error messages look intimidating at first, but they follow patterns, and most of them tell you exactly what went wrong and where. Learning to read them turns debugging from guesswork into a quick routine. You will meet them in three places: the terminal running your dev server, the browser console, and the error overlay that development builds show on top of the page.
Where errors come from
| Type | When it appears | Example |
|---|---|---|
| Compile or syntax error | Before the app runs; shown in terminal and overlay | Unexpected token, adjacent JSX elements must be wrapped |
| Runtime error | While a component renders or an event runs | Cannot read properties of undefined |
| Warning | App still runs, but React spots a problem | Each child in a list should have a unique key prop |
Reading the message from top to bottom
Every error has three useful parts. The first line states the problem in plain words. Below it is a file name with a line and column number, which points at the exact spot. Then comes a stack trace or component stack listing which functions or components were active. Start with the first line and the location; the stack is only needed when the location is inside a library rather than your own code.
Decoding common messages
// Error: Cannot read properties of undefined (reading 'map')
function ProductList({ products }) {
return (
<ul>
{products.map((p) => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}
// Cause: the parent rendered <ProductList /> without the products prop,
// or the data has not loaded yet. Fix with a default value:
function SafeProductList({ products = [] }) {
return (
<ul>
{products.map((p) => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}
"Cannot read properties of undefined (reading 'map')" means the thing before .map is undefined. The word in brackets tells you which property was being read, so search backwards from there to see why the value is missing.
"Adjacent JSX elements must be wrapped in an enclosing tag" means a component returns two siblings without a parent. Wrap them in a fragment.
"Objects are not valid as a React child" means you tried to render a whole object, such as {user}, instead of one of its fields like {user.name}. Dates are a frequent culprit; convert them with toLocaleDateString() first.
"Too many re-renders" usually means a state setter is being called during render, often written as onClick={setCount(1)} instead of onClick={() => setCount(1)}.
The trap of fixing the wrong line
The reported line is where the error surfaced, not always where it started. If products is undefined inside a child, the bug is often in the parent that forgot to pass it, or in the fetch that has not completed. Fixing the symptom with ?. everywhere can hide a real data problem. Check where the value should come from before patching the line that crashed.
Another trap is reading only the overlay and ignoring warnings in the console. Warnings such as missing keys do not crash the app but lead to subtle bugs later.
A quick routine
- Read the first line slowly and note the key word (undefined, not a function, wrapped).
- Jump to the file and line shown.
- Ask where the failing value comes from and trace it back one step.
- Fix, save and let the dev server reload.
Takeaway: read the first line and location first, translate the message into "which value was wrong", and trace that value to its source before changing code.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.