react js counter example

React JS Counter Example

By Christopher Williams
AD

Welcome to the world of React JS! If you're new to this popular JavaScript library or looking to enhance your skills, creating a simple counter application is an excellent starting point. Not only does it help you understand the basics, but it also serves as a foundation for building more complex apps. In this guide, we'll create a React JS counter example step by step, optimizing the process for search engine visibility.

Before we dive in, ensure you have Node.js and npm installed on your computer. Then, initialize a new React app using Create React App, a popular bootstrapping tool. Open your terminal, type 'npx create-react-app my-counter-app' (replacing 'my-counter-app' with the name you prefer), and hit Enter. Once the app is set up, navigate to the app's directory using 'cd my-counter-app'.

Setting Up the Counter App

First, let's set up the basic structure of our counter app. In the 'src' folder, you'll find an index.js file. Open it, and you'll see a default React app template. Import React and useState hook at the beginning to manage state in our functional component.

AD
Building an Animated Counter with React and CSS
Building an Animated Counter with React and CSS
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
React JS Cheat Sheet
React JS Cheat Sheet

To make our app SEO-friendly, use descriptive titles and meta descriptions. Install the Helmet library to manage these elements easily. Run 'npm install react-helmet' in your terminal, then import it in your index.js file. Wrap your App component with HelmetProvider and add a title to the Helmet component.

Creating the Counter Component

Create a new file called 'Counter.js' in the 'src' folder. Here, we'll build our Counter component. Start by importing React, useState, and use a functional component to create a new counter.

How to Call Function in React JS
How to Call Function in React JS
React Cheat Sheet, React Coding, React Programming, Free Programming Books, React Code, Computer Science Programming, Css Tutorial, Coding Lessons, Learn Web Design
React Cheat Sheet, React Coding, React Programming, Free Programming Books, React Code, Computer Science Programming, Css Tutorial, Coding Lessons, Learn Web Design
ReactJS Pagination Tutorial: Building from Scratch [VIDEO]
ReactJS Pagination Tutorial: Building from Scratch [VIDEO]

Initialize the state with a count value using the useState hook. Set the initial count to 0. We'll also need buttons to increment and decrement the count. Create two functions, 'incrementCount' and 'decrementCount', that update the count using the setCount function provided by the useState hook.

Displaying the Counter Value

To display the current count, use JSX to render the 'count' state variable. You can wrap it with a tag to make it stand out. Remember, in React, you can't change the DOM directly; instead, update the state, and React will re-render your component with the new value.

AD

Now, wrap your Counter component with the Helmet component to add SEO-friendly meta tags. Set the title to 'My Counter App' and add a meta description explaining what the app does. Finally, export the Counter component to use it in our App component.

Implementing the Counter in the App

Back in the 'index.js' file, import the Counter component and wrap it with a

element. Pass the incrementCount and decrementCount functions as props to the Counter component.

Now, when you run the app using 'npm start', you should see a simple counter app with two buttons, 'Increment' and 'Decrement', and a count value displayed in the middle. Clicking these buttons will change the count, demonstrating React's state management and component-based architecture.

Styling the Counter App

To make our app more visually appealing, let's add some styling. Create a new file called 'styles.css' in a 'src/styles' folder (create the 'styles' folder if it doesn't exist). Import this CSS file in our index.js file and style the Counter component.

You can add basic styles like colors, font sizes, and button appearances. The key here is not to overdo it, ensuring the app remains user-friendly and easy to navigate. Use clear, descriptive class names and IDs for better SEO and accessibility.

Final Touches and SEO Optimization

For better SEO, add alt tags to any images or icons used in the app. Ensure the app's structure is clean and logical, with descriptive file names and folders. Also, use semantic HTML elements where possible to improve accessibility and search engine understanding of your app.

In the final touch, consider adding a subtle call-to-action or a forward-looking thought in your app. Perhaps you could suggest users share the app on social media or encourage them to explore other features or technologies you work with. This helps keep users engaged and drives further interaction with your content.

And there you have it – a simple yet engaging React JS counter example! By following these steps, you've not only created a useful app but also optimized it for SEO. Happy coding, and remember, the more you practice, the more proficient you become!

React JS Context API - beautiful coding images💯🥰
React JS Context API - beautiful coding images💯🥰
React projet setup
React projet setup
Learning React: The Main Concepts
Learning React: The Main Concepts
React
React
Top 7 React Hooks Every Developer Should Know
Top 7 React Hooks Every Developer Should Know
Input Character Counter JS - Code Snippet
Input Character Counter JS - Code Snippet
React Form Handling 2026 — React Hook Form vs Server Actions vs TanStack Form
React Form Handling 2026 — React Hook Form vs Server Actions vs TanStack Form
NEXT.JS VS REACT (2026)
NEXT.JS VS REACT (2026)
REACT VS NEXT.JS (2026)
REACT VS NEXT.JS (2026)
4 Favorite Features of React Js Development
4 Favorite Features of React Js Development
React.js Cheat Sheet 2026 ⚛️ | Complete React Hooks, Components & JSX Quick Reference
React.js Cheat Sheet 2026 ⚛️ | Complete React Hooks, Components & JSX Quick Reference
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
a board game with the words react on it and an image of a maze in front of
a board game with the words react on it and an image of a maze in front of
What are the Reactjs Properties
What are the Reactjs Properties
Let's Learn about React with iLearn for Free!!! React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications, as it is optimal for fetching rapidly changing data that needs to be recorded. Check the link to know more: https://ilearnclub.org/search?q=React #React #ReactJS #JavaScript #Nodejs #WebDeveloper #iLearn #WhereLe React Programming Tutorial, Reactjs Development Services, What Is React Programming, React Programming, Reactjs Development Company, Reactjs Development Advantages, Reactjs App Development, React Programming Tips, React Programming Guide
Let's Learn about React with iLearn for Free!!! React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications, as it is optimal for fetching rapidly changing data that needs to be recorded. Check the link to know more: https://ilearnclub.org/search?q=React #React #ReactJS #JavaScript #Nodejs #WebDeveloper #iLearn #WhereLe React Programming Tutorial, Reactjs Development Services, What Is React Programming, React Programming, Reactjs Development Company, Reactjs Development Advantages, Reactjs App Development, React Programming Tips, React Programming Guide
React Fundamental
React Fundamental
React Cheat Sheet You Must Save | Beginner Coding Guide
React Cheat Sheet You Must Save | Beginner Coding Guide
an image of a web page with the words react basics written in different font and numbers
an image of a web page with the words react basics written in different font and numbers
Read Everything About React JS | Click the Link below!
Read Everything About React JS | Click the Link below!
a screenshot of a web page with the text 1 usestate
a screenshot of a web page with the text 1 usestate
AD