Code Flow and Warnings
Understanding code flow in React means knowing the order in which your components render, update the DOM and run effects. Warnings are the yellow messages React prints in the browser console during development, pointing out code that works for now but is likely to cause bugs. Reading both helps you debug problems that do not crash the app but still make it behave strangely.
The order things happen in
When state or props change, React follows the same steps every time:
- Render: React calls your component function to get new JSX. This should be pure, with no side effects.
- Commit: React compares the new output with the previous one and updates only the DOM nodes that changed.
- Effects: after the browser paints, React runs
useEffectcallbacks whose dependencies changed. Before re-running an effect, it runs the previous effect's cleanup function.
Parents render before children, but child effects run before parent effects. Adding a console.log at the top of a component and inside its effect quickly shows you this order and how often each part runs.
function Profile({ userId }) {
console.log('render Profile', userId);
useEffect(() => {
console.log('effect: load user', userId);
return () => console.log('cleanup for', userId);
}, [userId]);
return <h2>User {userId}</h2>;
}
Why logs appear twice in development
If your app is wrapped in <StrictMode>, React deliberately renders components twice and mounts, unmounts and remounts effects once in development. The goal is to expose code that is not pure or effects that are missing cleanup. This does not happen in production builds. Seeing double logs is expected, not a bug in your code, though recent React versions dim the second log in the console.
Warnings you will meet early
| Warning (shortened) | What it means |
|---|---|
| Each child in a list should have a unique "key" prop | Items from map() need a stable key, such as a database id. |
| A component is changing an uncontrolled input to be controlled | The input's value started as undefined and later became a string. Initialise state with ''. |
| You provided a `value` prop without an `onChange` handler | The field is read-only. Add onChange or use defaultValue. |
| Cannot update a component while rendering a different component | A state setter is being called during render. Move it into an event handler or effect. |
| React Hook useEffect has a missing dependency | An ESLint rule spotted a variable used inside the effect but not listed in its dependency array. |
Clicking the small arrow next to a warning in the console expands a component stack, which lists the component tree leading to the problem. Read it from the top to find the file to fix.
The mistake: ignoring yellow text
Because warnings do not stop the app, it is tempting to scroll past them. But each one usually predicts a real bug. A missing key can make a list show the wrong item's state after reordering. A missing effect dependency means the effect keeps using stale values. Silencing the dependency rule with a comment instead of fixing the code is a common source of hard-to-find bugs. Treat a clean console as part of finishing a feature.
Using an array index as the key is another trap. It removes the warning, but if items can be inserted, removed or reordered, React will still mix up their state. Prefer a stable id from the data.
Takeaways
- Render, commit, then effects: keep render pure and put side effects in effects or handlers.
- Double logs in development come from StrictMode and are intentional.
- Read each warning and its component stack, and fix the cause rather than hiding it.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.