DeveloperBreeze

Building a Modern Web Application with React and Redux

Introduction

In this tutorial, we will learn how to build a modern web application using React and Redux. React is a popular JavaScript library for building user interfaces, while Redux is a predictable state container that helps manage application state. We'll cover the basics of setting up a React project, integrating Redux, and implementing a simple application to demonstrate these tools.

Prerequisites

Before starting this tutorial, you should have a basic understanding of JavaScript and familiarity with the command line. Additionally, ensure you have Node.js and npm (Node Package Manager) installed on your machine.

Step 1: Setting Up Your React Application

First, we need to set up a new React application. We'll use Create React App, a popular toolchain for setting up React projects with no build configuration.

  1. Create a New React App

Open your terminal and run the following command to create a new React application:

   npx create-react-app my-react-app

This command will create a new directory called my-react-app with all the necessary files and dependencies.

  1. Navigate to Your Project Directory
   cd my-react-app
  1. Start the Development Server
   npm start

This command starts the development server and opens your new React application in the browser.

Step 2: Installing Redux and React-Redux

To manage our application's state with Redux, we need to install Redux and the React-Redux bindings.

  1. Install Redux and React-Redux

Run the following command in your project directory:

   npm install redux react-redux

Step 3: Setting Up Redux

Now, let's set up Redux in our React application.

  1. Create a Redux Store

In the src directory, create a new file called store.js and add the following code:

   import { createStore } from 'redux';

   const initialState = {
     count: 0,
   };

   const reducer = (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(reducer);

   export default store;

This code sets up a simple Redux store with an initial state containing a count property and a reducer function to handle INCREMENT and DECREMENT actions.

  1. Provide the Redux Store to Your React Application

In the src/index.js file, wrap your <App /> component with the <Provider> component from react-redux, passing it the store as a prop.

   import React from 'react';
   import ReactDOM from 'react-dom/client';
   import { Provider } from 'react-redux';
   import store from './store';
   import App from './App';
   import './index.css';

   const root = ReactDOM.createRoot(document.getElementById('root'));
   root.render(
     <Provider store={store}>
       <App />
     </Provider>
   );

Step 4: Connecting React Components to Redux

Let's connect our React components to the Redux store to interact with the application state.

  1. Create a Counter Component

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

   import React from 'react';
   import { useSelector, useDispatch } from 'react-redux';

   const Counter = () => {
     const count = useSelector((state) => state.count);
     const dispatch = useDispatch();

     const increment = () => {
       dispatch({ type: 'INCREMENT' });
     };

     const decrement = () => {
       dispatch({ type: 'DECREMENT' });
     };

     return (
       <div>
         <h1>Counter: {count}</h1>
         <button onClick={increment}>Increment</button>
         <button onClick={decrement}>Decrement</button>
       </div>
     );
   };

   export default Counter;

This code defines a Counter component that uses the useSelector hook to access the count state and the useDispatch hook to dispatch actions.

  1. Use the Counter Component in Your App

Open the src/App.js file and add the Counter component:

   import React from 'react';
   import Counter from './Counter';

   const App = () => {
     return (
       <div className="App">
         <h1>React and Redux Counter</h1>
         <Counter />
       </div>
     );
   };

   export default App;

Conclusion

Congratulations! You have successfully set up a React application with Redux for state management. You created a simple counter application to demonstrate the integration of Redux in a React project. From here, you can explore more advanced concepts like middleware, asynchronous actions, and integrating APIs to build more complex applications.

Next Steps

  • Learn about Redux middleware like Redux Thunk for handling asynchronous actions.
  • Explore React Router for adding navigation to your application.
  • Integrate APIs to fetch and manage data in your application.

Related Posts

More content you might like

Article

Mastering Modern Web Development: Trends, Tools, and Tutorials for 2025 and Beyond

Web development in 2025 is all about creating fast, scalable, and user-friendly applications. Embracing trends like Jamstack, micro frontends, and AI integration will keep you ahead of the curve. By mastering modern tools like Next.js, Tailwind CSS, Docker, and Kubernetes, you’re not just building websites—you’re shaping the future of digital experiences.

Whether you’re following our step-by-step tutorials or exploring the latest tech trends, remember that the world of web development is vast and full of opportunities. Keep learning, keep experimenting, and most importantly, have fun creating amazing digital experiences.

Feb 11, 2025
Read More
Article

Integrating Flowbite with Tailwind CSS: A Step-by-Step Tutorial

   <!-- src/index.html -->

   <link rel="stylesheet" href="output.css">

Full Example:

Oct 24, 2024
Read More
Tutorial
javascript

Automatically Detect New Components in Storybook Without Restarting the Server

Here’s how you can set up your scripts:

{
  "scripts": {
    "storybook": "storybook -p 6006",        // Standard Storybook start
    "storybook:watch": "storybook -p 6006 --watch"  // Watch mode for dynamic changes
  }
}

Oct 10, 2024
Read More
Tutorial
javascript

التعامل مع JSON في JavaScript: قراءة البيانات وكتابتها

النتيجة:

{"name":"أحمد","age":30,"isMarried":false,"children":["سارة","علي"]}

Sep 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!