React Redux Counter Example: Step-by-Step Tutorial
React and Redux are two fundamental tools in modern web development, often used in tandem to manage state effectively in JavaScript applications. A common example of their combined use is creating a simple counter application. In this article, we'll guide you through building a React-Redux counter example, explaining key concepts and best practices along the way.
Let's begins by understanding the key components of this project: React for the view layer, Redux for state management, and React-Redux for binding these two libraries together.
Setting Up the Project
To start, install the necessary dependencies: React, Redux, and React-Redux.
First, initialize your project with npm or yarn. Then, navigate to your project directory and run `npm install react react-dom redux react-redux`.
Project Structure
Creating a simple folder structure for your project is essential. Here's a basic structure to follow:
- src: This is where your application's code resides.
- src/actions: Redux actions go here.
- src/reducers: Redux reducers go here.
- src/components: React components go here.
- App.js: The main application component.
- index.js: The entry point of your application.
Creating the Store
After setting up the project structure, let's create a Redux store. In your main `index.js` file, include the following code:
import { createStore } from 'redux';
import rootReducer from './reducers';
const store = createStore(rootReducer);
ReactDOM.render(
Creating Redux Actions
Now, let's create actions that will update our counter. In `src/actions/index.js`, include the following:
export const INCREMENT_COUNT = 'INCREMENT_COUNT';
export const decrementCount = {type: DECREMENT_COUNT,payload: 1);
export const incrementCount = {type: INCREMENT_COUNT,payload: 1);
Creating the Reducer
The reducer will handle updating the state based on the dispatched actions. In `src/reducers/index.js`, include the following:
const counterReducer = (state = 0, action) => {
switch (action.type) {
case INCREMENT_COUNT:
return state + action.payload;
case DECREMENT_COUNT:
return state - action.payload;
default:
return state;
}}
};
export default counterReducer;
Creating React Components
Let's create a simple React component for our counter. In `src/components/Counter.js`:
The Component Structure
The component structure involves a button to decrease, display the count, and another button to increase the count.
import React from 'react';
const Counter = () => (
{
const [count, setCount] = React.useState(0);
const decreaseCount = () => {
setCount(count - 1);
};
const increaseCount = () => {
setCount(count + 1);
};
return (
<div>
<button onClick={decreaseCount} -> Decrement</button>
<p>Count: {count}</p>
<button onClick={increaseCount} -> Increment</button>
</div>
);
};
export default Counter;
Connecting React and Redux
Finally, use the `connect` function from `react-redux` to connect our React component to the Redux store. In `src/components/CounterConnected.js`:
import { connect } from 'react-redux';
import Counter from './Counter';
const mapStateToProps = (state) => ({
count: state.counter
});
export default connect(mapStateToProps)(Counter);
In a simple and clean way, we've created a React-Redux counter example. The core principle is to separate the application's logic and views into different libraries and use `react-redux` to connect them. This pattern makes the application highly manageable and scalable.
Now that you've created a counter app using React and Redux, explore more complex examples and dive deeper into Redux middleware, tucked actions, and asyncredux to further enhance your understanding of Redux.