Code to start a new react project
Here are the steps to start a new React project:
Using create-react-app
create-react-app
is a popular tool that allows you to quickly create a new React project with a single command.
- Open your terminal or command prompt.
- Run the following command:
npx create-react-app my-app
(replacemy-app
with your desired project name). - Follow the prompts to set up your project:
- Choose a template (e.g., "default" or "typescript").
- Choose a package manager (e.g., "npm" or "yarn").
- Choose a testing library (e.g., "jest" or "mocha").
- Once the setup is complete, navigate to your project directory:
cd my-app
. - Start the development server:
npm start
(oryarn start
). - Open your web browser and navigate to
http://localhost:3000
to see your new React project in action.
Using a code editor or IDE
If you prefer to create a new React project from scratch, you can use a code editor or IDE like Visual Studio Code, IntelliJ IDEA, or Sublime Text.
- Create a new folder for your project and navigate to it in your terminal or command prompt.
- Initialize a new Node.js project:
npm init
(oryarn init
). - Install React and its dependencies:
npm install react react-dom
(oryarn add react react-dom
). - Create a new file called
index.js
and add the following code:import React from 'react'; import ReactDOM from 'react-dom';
const App = () => { return
Hello, World!
; };ReactDOM.render(
5. Create a new file called `index.html` and add the following code:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>My React App</title>
</head>
<body>
<div id="root"></div>
<script src="index.js"></script>
</body>
</html>
- Start the development server:
npm start
(oryarn start
). - Open your web browser and navigate to
http://localhost:3000
to see your new React project in action.
That's it! You now have a new React project up and running.