Skip to main content

Command Palette

Search for a command to run...

What is Redux and How can you apply it?

Updated
2 min read
What is Redux and How can you apply it?
M

A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.

Redux is a JavaScript library for managing application state. It is often used with React to build user interfaces, but can also be used with other libraries or frameworks.

The basic principles of Redux are:

  1. The entire application state is stored in a single JavaScript object, called the store.

  2. The store can only be modified by dispatching actions, which are plain JavaScript objects that describe a change to the state.

  3. The state changes are handled by pure functions called reducers, which take the current state and an action as arguments and return the next state.

To use Redux in a React application, you need to install the redux and react-redux libraries and configure them in your application.

  1. Create a store by calling the createStore function from the redux library, passing in a reducer function.

  2. Wrap your root component in a <Provider> component from the react-redux library, passing in the store as a prop.

  3. To access the state and dispatch actions from your component, you can use the useSelector and useDispatch hooks from the react-redux library.

  4. To update the state, you need to dispatch an action by calling the dispatch method on the useDispatch hook, passing in an action object.

  5. To handle the state update, you need to write a reducer function that takes the current state and an action as arguments and returns the next state.

Here's an example:

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

// create a store
const store = createStore(reducer);

// root component
function App() {
  const counter = useSelector(state => state.counter);
  const dispatch = useDispatch();

  return (
    <div>
      <h1>Counter: {counter}</h1>
      <button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment</button>
    </div>
  );
}

// wrap the root component in a provider
function Root() {
  return (
    <Provider store={store}>
      <App />
    </Provider>
  );
}

// define the reducer function
function reducer(state, action) {
  switch (action.type) {
    case 'INCREMENT':
      return { counter: state.counter + 1 };
    default:
      return state;
  }
}

More from this blog

DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

অনেকেই বলে — “ভাই, Web Development-এ DSA লাগে না” আসলে Junior Developer থাকলে এমনটাই মনে হয়।আমি নিজেও Junior থাকতে তাই ভাবতাম। আমার আবার একটা বাজে স্বভাব আছে 😅👉 কোন কিছুর বাস্তব প্রয়োজন না বুঝলে সেটা আমার মাথায় ঢুকে না। Junior থাকতে DSA শিখেছি...

Dec 24, 20253 min read
DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Rust-এ Garbage Collector (GC) নেই, কিন্তু মেমরি নিরাপত্তা (memory safety) আর performance Rust-এর সবচেয়ে শক্তিশালী দিক।তাই, Go-এর মতো runtime-based GC না থাকা সত্ত্বেও Rust compile-time এ মেমরি ম্যানেজ করে Ownership System, Borrow Checker, আর Lifetime...

Oct 5, 202523 min read
🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Morshedul Munna

45 posts

As a Software Developer, I am like an architect who designs and builds digital Products. I use my knowledge and expertise to create modern applications that are both efficient and elegant.