A step-by-step tutorial to build your first React application from scratch.
React is a popular JavaScript library for building user interfaces, particularly single-page applications (SPAs). It allows you to create reusable components and manage the state of your application efficiently. This guide will walk you through building your first React application step by step.
Before you start, ensure you have the following installed:
Verify the installation:
node -v
npm -v
Use Create React App (CRA) to set up a new React project quickly.
npm install -g create-react-app
npx create-react-app my-first-react-app
cd my-first-react-app
npm start
This will open your app in the browser at http://localhost:3000
.
Your project will have the following structure:
my-first-react-app/
├── node_modules/
├── public/
│ ├── index.html
│ └── ...
├── src/
│ ├── App.js
│ ├── index.js
│ └── ...
├── package.json
└── ...
public/index.html
: The main HTML file.src/index.js
: The entry point for your React app.src/App.js
: The main component of your app.React applications are built using components. Let’s create a simple component.
src/App.js
Replace the content of App.js
with the following:
import React from 'react';
function App() {
return (
<div className="App">
<h1>Hello, React!</h1>
<p>Welcome to my first React application.</p>
</div>
);
}
export default App;
Save the file, and your browser will automatically reload to show the updated content.
Let’s add a button that updates a counter.
useState
HookReact’s useState
hook allows you to add state to functional components.
Update App.js
:
import React, { useState } from 'react';
function App() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div className="App">
<h1>Hello, React!</h1>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
export default App;
Click the button to see the counter increase.
Let’s create a separate component for the counter.
Create a file named Counter.js
in the src
folder:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
export default Counter;
Update App.js
to use the Counter
component:
import React from 'react';
import Counter from './Counter';
function App() {
return (
<div className="App">
<h1>Hello, React!</h1>
<Counter />
</div>
);
}
export default App;
You can style your React app using CSS or CSS-in-JS libraries.
Create a file named App.css
in the src
folder:
.App {
text-align: center;
padding: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
Update App.js
to import the CSS file:
import React from 'react';
import Counter from './Counter';
import './App.css';
function App() {
return (
<div className="App">
<h1>Hello, React!</h1>
<Counter />
</div>
);
}
export default App;
Once your app is ready, you can deploy it to a hosting service like Netlify, Vercel, or GitHub Pages.
npm run build
This creates an optimized production build in the build
folder.
build
folder into the Netlify dashboard.By following this guide, you’ve built your first React application and learned the basics of components, state, and interactivity. Keep practicing and exploring to become a React pro! 🚀