JSX Rules and Best Practices
Learn the essential JSX syntax rules, common mistakes, and best practices for writing clean and readable React code.
1. Introduction to JSX Rules
JSX allows developers to describe user interfaces using HTML-like syntax inside JavaScript. However, JSX follows specific syntax rules that must be followed to write valid React components.
function App() {
return (
<div>
<h2>Welcome to CIIT Training Institute</h2>
<p>Learn React step by step.</p>
</div>
);
}
export default App;
2. Return One Parent Element
A component must return one JSX element. When you need multiple sibling elements, wrap them in a parent element or a React Fragment.
return (
<h2>Student Details</h2>
<p>Samadhan</p>
);
Multiple sibling elements are returned without a common parent.
return (
<div>
<h2>Student Details</h2>
<p>Samadhan</p>
</div>
);
Both elements are inside one parent.
<>...</> when an additional
DOM wrapper is unnecessary.
3. Close Every JSX Tag
Every JSX element must have a closing tag or use self-closing syntax when appropriate. This also applies to elements such as images and input fields.
function App() {
return (
<div>
<h2>Student Profile</h2>
<p>Student Name: Samiksha</p>
<input type="text" placeholder="Enter name" />
<img src="/images/student.png"
alt="Student profile" />
</div>
);
}
export default App;
Student Profile
Student Name: Samiksha
The example image path is illustrative. Add the image to your React application's assets when implementing this example.
4. Use className Instead of class
JSX uses className to assign CSS classes
to elements. This follows JavaScript DOM property naming
conventions.
<h2 class="student-title">Samadhan</h2>
<h2 className="student-title">Samadhan</h2>
function App() {
return (
<div className="student-card">
<h2 className="student-title">
Samadhan
</h2>
<p className="student-description">
Learning React at CIIT Training Institute.
</p>
</div>
);
}
export default App;
5. Use htmlFor for Labels
In JSX, use htmlFor instead of the HTML
for attribute when associating a label
with a form control.
function App() {
return (
<div>
<label htmlFor="studentName">
Student Name
</label>
<input
id="studentName"
type="text"
placeholder="Enter student name"
/>
</div>
);
}
export default App;
Expected Browser Output
6. Use Curly Braces for Expressions
JavaScript variables and expressions can be inserted into JSX using curly braces. Use quotation marks for literal strings and curly braces for JavaScript values.
function App() {
const studentName = "Pradnya";
const completedLessons = 8;
const totalLessons = 12;
return (
<div>
<h2>Student Progress</h2>
<p>Name: {studentName}</p>
<p>Completed: {completedLessons}</p>
<p>Remaining: {totalLessons - completedLessons}</p>
</div>
);
}
export default App;
Student Progress
Name: Pradnya
Completed: 8
Remaining: 4
7. Use Correct Event Names
JSX event handler names commonly use camelCase, such as
onClick. Pass a function to the event
attribute instead of calling it immediately.
function App() {
function handleClick() {
alert("Welcome, Samiksha!");
}
return (
<button onClick={handleClick}>
Show Welcome Message
</button>
);
}
export default App;
onclick={handleClick}
onClick={handleClick()}
onClick={handleClick}
Pass the function reference to the event handler.
8. Use Keys When Rendering Lists
When rendering arrays of elements, provide a stable and unique key among sibling items. Keys help React identify items when a list changes.
function App() {
const students = [
{ id: 1, name: "Samadhan" },
{ id: 2, name: "Samiksha" },
{ id: 3, name: "Pradnya" }
];
return (
<div>
<h2>Student List</h2>
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
</div>
);
}
export default App;
Expected Browser Output
Student List
- Samadhan
- Samiksha
- Pradnya
9. Follow JSX Style Rules
The JSX style attribute expects a JavaScript
object. CSS property names are generally written in
camelCase, and string values are used for colors.
function App() {
const headingStyle = {
color: "blue",
backgroundColor: "lightyellow",
padding: "12px",
borderRadius: "8px"
};
return (
<h2 style={headingStyle}>
Welcome, Samadhan!
</h2>
);
}
export default App;
style={headingStyle} to pass a JavaScript
object. Do not pass a plain CSS string to the JSX
style attribute.
10. Keep Components Clean and Readable
Write small, focused components, use meaningful variable names, and keep JSX indentation consistent. Readable code is easier to maintain, test, and debug.
function StudentCard() {
const studentName = "Pradnya";
const courseName = "React JS";
return (
<div className="student-card">
<h2>{studentName}</h2>
<p>Course: {courseName}</p>
</div>
);
}
export default StudentCard;
11. Practice Questions
- Correct a component that returns multiple sibling elements without a parent.
- Fix missing closing tags in a JSX component.
- Create a student profile using
classNameandhtmlFor. - Display Samiksha's course progress using JavaScript expressions.
- Render Samadhan, Samiksha, and Pradnya using
map()and stable keys. - Create a button with an
onClickevent handler. - Apply inline styles through a JavaScript object.
Summary
JSX follows specific rules for returning elements, closing tags, naming attributes, and embedding JavaScript expressions. Use one parent element or a Fragment for sibling elements, className for CSS classes, htmlFor for labels, and camelCase for event handlers. Use stable keys when rendering lists, and write clean, readable components to make React applications easier to maintain.