Day 3 -Building Your First React JS Application:
Start building your first ReactJS application by creating components and using JSX to define their structure
ReactJS is a popular JavaScript library used for building web applications. In this article, we will learn how to create your first ReactJS application by creating components and using JSX to define their structure.
To get started, you will need to set up your development environment as described in our previous article. Once you have your development environment set up, follow these steps to create your first ReactJS application:
Create a new ReactJS project
To create a new ReactJS project, open your terminal and run the following command:
npx create-react-app my-app
This command will create a new ReactJS project called “my-app” in your current directory.
Create a new component
Once your project is created, you can start building your first ReactJS component. Components are the building blocks of a ReactJS application, and they can be reused throughout your application. To create a new component, create a new file called “MyComponent.js” in the “src” directory of your project.
import React from 'react';
function MyComponent() {
return (
<div>
<h1>Hello, World!</h1>
</div>
);
}
export default MyComponent;
In this example, we have created a new component called “MyComponent” that returns a simple HTML element. The HTML element is defined using JSX, which allows you to write HTML-like syntax in your JavaScript code.
Render your component
To render your component, open the “index.js” file in the “src” directory of your project and replace the existing code with the following:
import React from 'react';
import ReactDOM from 'react-dom';
import MyComponent from './MyComponent';
ReactDOM.render(<MyComponent />, document.getElementById('root'));
In this example, we have imported our “MyComponent” component and rendered it using the “ReactDOM.render” method. This method takes two arguments: the component to render and the DOM element to render it in.
Run your application
To run your application, open your terminal and run the following command:
npm start
This command will start a development server and open your application in a web browser. You should see the text “Hello, World!” displayed on the page.
Congratulations, you have just created your first ReactJS application! You can now start building more complex components and adding functionality to your application.
Conclusion
In this article, we learned how to create your first ReactJS application by creating components and using JSX to define their structure. We also learned how to render components and run our application. ReactJS is a powerful library that allows you to build complex web applications with ease. By learning the basics of ReactJS, you can start building your own applications and taking advantage of the many features and tools that ReactJS has to offer.