Skip to content
DeveloperMemos

Creating a Counter App in React

React, JavaScript, Frontend Development2 min read

React is a popular JavaScript library for building user interfaces. It allows developers to create reusable UI components that efficiently update and render when the underlying data changes. In this tutorial, we will walk through the process of creating a counter app using React.

Prerequisites

Before getting started, make sure you have Node.js and npm (Node Package Manager) installed on your machine. You can download them from the official Node.js website. Once you have Node.js and npm set up, you're ready to begin.

Setting Up a New React Project

To create a new React project, open your terminal and navigate to the directory where you want to create the project. Run the following command to initialize a new React application:

1npx create-react-app counter-app

This will create a new directory called counter-app with the basic structure and dependencies of a React project.

Next, navigate to the project directory:

1cd counter-app

You can now start the development server by running the following command:

1npm start

Once the server starts, you should see the default React app running at http://localhost:3000. Open it in your web browser to confirm that everything is set up correctly.

Creating the Counter Component

In React, applications are built using components. Components are reusable building blocks that encapsulate the UI and logic of a specific part of the application. Let's create a Counter component that will handle our counter functionality.

In the src directory of your project, create a new file called Counter.js. Open the file and add the following code:

1import React, { useState } from 'react';
2
3const Counter = () => {
4 const [count, setCount] = useState(0);
5
6 const increment = () => {
7 setCount(count + 1);
8 };
9
10 const decrement = () => {
11 setCount(count - 1);
12 };
13
14 return (
15 <div>
16 <h2>Counter App</h2>
17 <p>Count: {count}</p>
18 <button onClick={increment}>Increment</button>
19 <button onClick={decrement}>Decrement</button>
20 </div>
21 );
22};
23
24export default Counter;

In the code above, we import the necessary functions from the react package: React and useState. The useState function is a React hook that allows us to add state to functional components. We use it to create a count state variable and a corresponding setCount function to update its value.

The increment and decrement functions modify the count state variable by calling setCount with the updated value. Finally, we render the current count value and two buttons that trigger the increment and decrement functions respectively.

Integrating the Counter Component

Now that we have our Counter component, let's integrate it into our main app component. Open the src/App.js file and replace its content with the following code:

1import React from 'react';
2import Counter from './Counter';
3
4const App = () => {
5 return (
6 <div className="App">
7 <Counter />
8 </div>
9 );
10};
11
12export default App;

In the code above, we import the Counter component and render it within the App component. Save the file, and you should see the counter app displayed in your browser.

Testing the Counter App

To test the counter app, open src/App.test.js and replace its content with the following code:

1import { render, screen } from '@testing-library/react';
2import App from './App';
3
4test('renders counter app', () => {
5 render(<App />);
6 const linkElement = screen.getByText(/counter app/i);
7 expect(linkElement).toBeInTheDocument();
8});

This test checks if the counter app is rendered correctly by searching for the text "Counter App" on the screen. Run the test using the following command:

1npm test

If everything is set up correctly, the test should pass without any failures.

In Closing

Congratulations! You have successfully created a simple counter app using React. In this tutorial, we learned about React components, state management with the useState hook, and event handling in React. We created a Counter component that maintains a count state variable and provides buttons to increment and decrement the count. Also remember, React's strength lies in its component-based architecture and efficient rendering. By breaking down your UI into reusable components and managing state effectively, you can create complex applications with ease. Happy coding!