Create a new react.js app
Here's a step-by-step guide to create a new React.js app:
Prerequisites:
- Node.js installed on your machine (you can download it from https://nodejs.org/en/download/)
- A code editor or IDE of your choice (e.g., Visual Studio Code, IntelliJ IDEA, etc.)
Step 1: Create a new project folder
Open your terminal or command prompt and create a new folder for your project:
mkdir my-react-app
Step 2: Initialize the project with npm
Navigate into the project folder and initialize the project with npm (Node Package Manager):
cd my-react-app
npm init
Follow the prompts to fill in the required information, such as the project name, version, and author.
Step 3: Install React and other dependencies
Install React and other dependencies required for the project:
npm install react react-dom
This will install React and React DOM, which is a library that allows you to render React components in the browser.
Step 4: Create a new React app
Create a new React app using the create-react-app
command:
npx create-react-app my-react-app
This will create a new React app with the default configuration and a basic file structure.
Step 5: Start the development server
Start the development server by running:
npm start
This will start the development server and open your app in your default web browser.
Step 6: Create a new component
Create a new component by creating a new file in the src
folder, for example, HelloWorld.js
:
// src/HelloWorld.js
import React from 'react';
function HelloWorld() {
return <h1>Hello, World!</h1>;
}
export default HelloWorld;
This is a simple component that renders an <h1>
element with the text "Hello, World!".
Step 7: Render the component
Render the component in the App.js
file, which is the main entry point of the app:
// src/App.js
import React from 'react';
import HelloWorld from './HelloWorld';
function App() {
return <HelloWorld />;
}
export default App;
This will render the HelloWorld
component in the app.
That's it! You now have a basic React app up and running. You can start building your app by creating more components, adding functionality, and styling your app using CSS or a CSS-in-JS library like Styled Components.
Here's the complete file structure:
my-react-app/
node_modules/
react/
react-dom/
...
public/
index.html
src/
App.js
HelloWorld.js
index.js
package.json