How to write comments in ReactJS ? (original) (raw)
Last Updated : 24 Apr, 2025
When working with **ReactJS or any other **programming language, making the code easy to understand is essential. One of the best ways to do this is by using comments. **Comments are simple notes within the code that help to explain what is happening or why something was done in a specific way.
To write comments in ReactJS, we can use simple JavaScript comments as well as the JSX objects as per our requirement.
In ReactJS, there are two main ways to write comments:
To write comments in React for JavaScript code, like functions and classes, we will be using JavaScript comments. We can use single-line as well as multiline comments using JavaScript.
- **Single-line comments: Use // for short comments on a single line.
- **Multi-line comments: Use /* ... */ for longer explanations or to comment out multiple lines.
**Now, let us understand with the help of the example:
JavaScript `
import React, { Component } from 'react'; // This is a single line comment class App extends Component { /* This is a multiline comment*/ render() { return (
Welcome to GeeksforGeeks
`
**Output:
2. Using JSX object
JSX is the syntax React uses to write HTML-like code inside JavaScript. When writing JSX, the way to add comments is a bit different. React requires comments to be placed inside curly braces {} and uses /* ... */ for multi-line comments.
**Example: The above example does not work when we want to comment things inside the **render block. This is because we use inside the render block and must use multi-line comment in curly braces {/* */}.
JavaScript `
import React, { Component } from 'react';
class App extends Component {
render() {
return (
Welcome to GeeksforGeeks
`
**Output
**Note: We must remember, that even though JSX gets rendered just like normal HTML, it is actually a syntax extension to JavaScript. So, using as we did with HTML and XML will not work.
JavaScript `
// Filename - App.js
import React, { Component } from 'react';
class App extends Component {
render() {
return (
{/* THIS ONE IS A VALID COMMENT */}
<h1>Welcome to GeeksforGeeks</h1>
</div>
);
}
}
export default App;
`
**Output
- **Comment Why, Not What: Focus on explaining why you're doing something, not just describing what the code is doing. This helps others understand the reasoning behind your approach.
- **Avoid Redundant Comments: Don’t add comments for code that is self-explanatory. If the code is clear, a comment may not be necessary.
- **Keep Comments Relevant: Only comment on complex or unclear sections of code. This helps avoid clutter and ensures comments are helpful.
- **Update Comments Regularly: Always update comments when modifying the code. Outdated comments can mislead others and cause confusion.
Conclusion
In ReactJS, adding comments is important for making the code easier to understand. There are two main ways to add comments: using regular JavaScript comments for functions and logic, and using JSX comments inside curly braces for the render block.