To install React, you can follow these steps:
- Make sure you have Node.js installed on your computer. You can download it from the official Node.js website and follow the installation instructions for your operating system.
- Once Node.js is installed, you can open a command prompt or terminal window.
- Create a new directory for your React project. You can do this by running the following command:
mkdir my-react-app
- Change into the new directory:
cd my-react-app
- Initialize a new Node.js project by running the following command:
npm init -y
- Now, you can install React and its dependencies. Run the following command:
npm install react react-dom
- Once the installation is complete, you can start writing React code in your project. You can create a new JavaScript file, such as
index.js
, and import React as necessary.Here’s a simple example to get you started:
import React from 'react'; import ReactDOM from 'react-dom'; const App = () => { return <h1>Hello, React!</h1>; }; ReactDOM.render(<App />, document.getElementById('root'));
- To see your React app in action, you need to create an HTML file, such as
index.html
. Add adiv
element with anid
of “root” where your React app will be rendered.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My React App</title> </head> <body> <div id="root"></div> <script src="index.js"></script> </body> </html>
- Finally, open
index.html
in a web browser, and you should see “Hello, React!” displayed on the page.
That’s it! You have successfully installed React and set up a basic React project. Happy coding!