Common JSX Mistakes Beginners Make (and How to Avoid Them)
JSX has a set of predictable beginner mistakes. Here are the most common ones and how to avoid each.
Common JSX Mistakes Beginners Make (and How to Avoid Them)
JSX looks familiar, which is why beginners make the same mistakes repeatedly. Here are the most common ones and how to avoid them.
Using class Instead of className
JSX is JavaScript, so class is a reserved word. Use className for the HTML class attribute and htmlFor for the for attribute.
Forgetting to Close Tags
In HTML, some tags like <img> are self-closing by convention. In JSX, every tag must be closed, either with a closing tag or as a self-closing tag with a slash.
Using Statements Instead of Expressions
You cannot put an if statement inside JSX curly braces. Use a ternary for conditions, or compute the value before the return statement.
Returning Multiple Root Elements
A component must return a single root. Returning sibling elements without a wrapper throws an error. Use a fragment to return siblings without an extra DOM node.
Using style as a String
In HTML, style is a string. In JSX, the style attribute takes an object with camelCased property names, like backgroundColor, not background-color.
Keys in Lists
When mapping arrays to elements, every item needs a unique key. Forgetting keys or using the array index causes subtle bugs when the list changes.
Conditional Rendering With &&
A common bug is rendering 0 or an empty string because && returns the left operand when it is falsy. Be explicit about what you want to render in each branch.
The Takeaway
Most JSX errors come from treating it like HTML. Remember it is JavaScript: close all tags, use camelCase, use expressions only, and always provide keys in lists.
Because JSX is JavaScript and class is a reserved word. Use className for the HTML class attribute and htmlFor for the for attribute to avoid the conflict.
Yes. In JSX, every tag must be closed, either with a closing tag or as a self-closing tag with a slash. Tags like img and input that are self-closing in HTML still need the closing slash in JSX.
No. JSX curly braces accept expressions, not statements. Use a ternary for conditions inside JSX, or compute the value before the return statement and reference it in curly braces.
Unlike HTML, the style attribute in JSX takes an object with camelCased property names, like backgroundColor. It does not take a string. This is because JSX is JavaScript.
Keys help React identify which items changed. When you map an array to elements, every item needs a unique stable key. Forgetting keys or using the array index can cause subtle bugs when the list is reordered or updated.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

