Thursday, November 28, 2024
Google search engine
HomeLanguagesHow to write comments in ReactJS ?

How to write comments in ReactJS ?

ReactJS is a JavaScript library used for building user interfaces. It is Declarative, Component-based and Technology stack agnostic. It has been designed for speed, simplicity, and scalability. These features make it one of the most popular libraries among web developers.

In this article, we will explore creating a React application and adding comments to it.

Creating React Application

Step 1: Create a React application with the following command:

npx create-react-app my-app

Step 2: Move to the project folder my-app with the following command:

cd my-app

 

Step 3: Start the development server with the following command:

yarn start

(This will run your application at localhost:3000, where you can see all the changes you make to the application.)

Project Structure: It will look like the following.

 

 

Example 1: Comments for React Components. We can write comments in React using the double forward-slash // or the asterisk format /* */, similar to regular JavaScript.

App.js




import React, { Component } from 'react';
  
// This is a comment
  
class App extends Component {
  
    /* This is 
    also a comment*/
    render() {
        return (
            <div>
                <h1>Welcome to neveropen</h1>
            </div>
        );
    }
}
  
export default App;


Output: 

 

Example 2: The above example does not work when we want to comment things inside the render block. This is because we use JSX inside the render block and must use multi-line comment in curly braces {/* */}.

App.js




import React, { Component } from 'react';
  
class App extends Component {
    render() {
        return (      
            <div>
                // This is not a valid comment
                /* Neither is this */
  
                {/* THIS ONE IS A VALID COMMENT */}
                  
                <h1>Welcome to neveropen</h1>
            </div>
        );
    }
}
  
export default App;


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.

App.js




import React, { Component } from 'react';
  
class App extends Component {
    render() {
        return (     
            <div>
                <!-- This is not a valid comment -->
                  
                {/* THIS ONE IS A VALID COMMENT */}
                  
                <h1>Welcome to neveropen</h1>
            </div>
        );
    }
}
  
export default App;


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments