redux react counter example

Mastering Redux with React: Counter Example Tutorial

By Amanda Hernandez
AD

Redux, a predictable state container for JavaScript apps, is often used alongside React, a popular JavaScript library for building user interfaces. A common use case is creating a counter application, demonstrating how Redux and React can work together seamlessly. Let's dive into a simple yet comprehensive example of a Redux React counter.

The primary goal of this example is to illustrate how Redux helps manage the state of our React application in a centralized and predictable way. By the end of this guide, you'll be able to build and understand a functional counter app using Redux and React.

Setting Up the Project

First, let's set up our project using Create React App and Redux:

AD
React projet setup
React projet setup
Efficient State Management in React with Redux and React Hooks
Efficient State Management in React with Redux and React Hooks
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
```bash npx create-react-app redux-react-counter cd redux-react-counter npm install redux react-redux ```

The above commands create a new React application and install Redux and the necessary React-Redux bindings.

Setting up the project foundation is crucial before we dive into implementing our counter app.

Components example in reacts and nextjs 14
Components example in reacts and nextjs 14
React Context API vs Redux: When and How to Use Each – Djamware
React Context API vs Redux: When and How to Use Each – Djamware
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD

Creating the Redux Store

In Redux, the store is an object that holds the entire state tree of your application. Let's create a store to manage our counter's state:

```javascript import { createStore } from 'redux'; const initialState = { count: 0, }; function counterReducer(state = initialState, action) { switch (action.type) { case 'INCREMENT': return { ...state, count: state.count + 1 }; case 'DECREMENT': return { ...state, count: state.count - 1 }; default: return state; } } const store = createStore(counterReducer); ```

Here, we've created a simple reducer that handles two actions, 'INCREMENT' and 'DECREMENT', modifying the `count` property of our state.

AD

Dispatching Actions

Actions are payloads of information that send data from your application to your store. Let's create two action creators for incrementing and decrementing the counter:

React
React
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
Setting up Redux store in Your React application
Setting up Redux store in Your React application
Redux tutorial for beginners
Redux tutorial for beginners
the react component diagram is shown in this image
the react component diagram is shown in this image
React Fundamental
React Fundamental
```javascript function increment() { return { type: 'INCREMENT' }; } function decrement() { return { type: 'DECREMENT' }; } ```

You can now dispatch these actions to change the state in your Redux store.

Connecting Redux to React

Now that we have our Redux store set up, let's connect it to our React application.

AD

First, wrap your root component (App) with the `Provider` component from `react-redux` to make the store accessible to all child components:

React Redux Full Course for Beginners | Redux Toolkit Complete Tutorial
React Redux Full Course for Beginners | Redux Toolkit Complete Tutorial
react server components are connected to each other with the same symbol in front of them
react server components are connected to each other with the same symbol in front of them
30 REACT INTERVIEW QUESTIONS EVERY DEVELOPER SHOULD PRACTICE
30 REACT INTERVIEW QUESTIONS EVERY DEVELOPER SHOULD PRACTICE
React – The Complete Guide | Learn React, Next.js & Redux
React – The Complete Guide | Learn React, Next.js & Redux
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
React With Redux Tutorial | React Redux Tutorial For Beginners | ReactJS For Beginners | Simp...
React With Redux Tutorial | React Redux Tutorial For Beginners | ReactJS For Beginners | Simp...
React JS Cheat Sheet
React JS Cheat Sheet
React Component Lifecycle — Complete Visual Guide (Class + Hooks)
React Component Lifecycle — Complete Visual Guide (Class + Hooks)
⚛️ Still confused about Redux? 🤔 Save this post and you'll never forget the Redux flow again. 💜 In this infographic you'll learn: ✅ What Redux is ✅ Why Redux is used ✅ Shopping Cart Example ✅ Redux Flow ✅ Central Store Concept ✅ No Prop Drilling If you're learning React, this is a must-save post. 🚀 💬 Have you used Redux or Redux Toolkit in any project? Let me know in the comments. 📌 Save it for later. 📤 Share with your developer friends. ❤️ Follow @thenitishdev for more developer content... Let It Be
⚛️ Still confused about Redux? 🤔 Save this post and you'll never forget the Redux flow again. 💜 In this infographic you'll learn: ✅ What Redux is ✅ Why Redux is used ✅ Shopping Cart Example ✅ Redux Flow ✅ Central Store Concept ✅ No Prop Drilling If you're learning React, this is a must-save post. 🚀 💬 Have you used Redux or Redux Toolkit in any project? Let me know in the comments. 📌 Save it for later. 📤 Share with your developer friends. ❤️ Follow @thenitishdev for more developer content... Let It Be
NEXT.JS VS REACT (2026)
NEXT.JS VS REACT (2026)
The only introduction to Redux (and React-Redux) you’ll ever need
The only introduction to Redux (and React-Redux) you’ll ever need
an image of a computer screen with the words react redux workflow - graphical chart sheet
an image of a computer screen with the words react redux workflow - graphical chart sheet
Redux easy explanation 😎 React Redux Beginners Guide
Redux easy explanation 😎 React Redux Beginners Guide
I created the exact same app with Redux and React. Here are the differences.
I created the exact same app with Redux and React. Here are the differences.
```javascript import { Provider } from 'react-redux'; import { store } from './store'; ReactDOM.render( , document.getElementById('root') ); ```

Creating the Counter Component

Let's create a simple Counter component that displays the current count and buttons to increment and decrement the counter:

```javascript import React from 'react'; import { connect } from 'react-redux'; import { increment, decrement } from './actions'; const Counter = ({ count, onIncrement, onDecrement }) => (

Count: {count}

); const mapStateToProps = (state) => ({ count: state.count, }); const mapDispatchToProps = { onIncrement: increment, onDecrement: decrement, }; export default connect(mapStateToProps, mapDispatchToProps)(Counter); ```

In this example, we're using `connect` from `react-redux` to pull in the count from our Redux store and make our action creators (`onIncrement` and `onDecrement`) available as props.

AD

Now, whenever you click the 'Increment' or 'Decrement' buttons, the respective action will be dispatched, updating the count in our Redux store and re-rendering our application with the new count.

That's it! You've now created a simple yet comprehensive Redux React counter example. Redux and React work together seamlessly to provide a predictable and component-based application state. Now you can expand on this foundation to build more complex applications.

AD