Redux là gì ? Cách sử dụng Redux trong 1 dự án react.

 Redux là một thư viện quản lý trạng thái phổ biến cho các ứng dụng JavaScript, đặc biệt là React. Redux giúp giải quyết vấn đề quản lý trạng thái trong ứng dụng, đặc biệt khi ứng dụng trở nên phức tạp và có nhiều thành phần cần chia sẻ dữ liệu. Bằng cách sử dụng Redux, bạn có thể quản lý trạng thái toàn cục một cách có tổ chức, giúp code dễ bảo trì và mở rộng hơn.
Lý do sử dụng Redux trong dự án react :

2. Các khái niệm cơ bản của Redux

Redux có một số khái niệm quan trọng bạn cần nắm rõ:

Store: Là nơi lưu trữ toàn bộ trạng thái của ứng dụng. Chỉ có một store duy nhất trong mỗi ứng dụng Redux.

State: Trạng thái ứng dụng được lưu trữ dưới dạng một đối tượng JavaScript.

Actions: Là các đối tượng chứa thông tin về loại hành động và dữ liệu cần thay đổi. Chúng được gửi đi để yêu cầu thay đổi trạng thái.

				
					const incrementAction = { type: 'INCREMENT' };
				
			

Reducers: Là các hàm thuần (pure functions) nhận vào state hiện tại và một action, sau đó trả về state mới dựa trên action đó.

				
					function counterReducer(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
}

				
			

Dispatch: Là cách để gửi một action tới store, yêu cầu store cập nhật trạng thái.

Nguồn:  https://www.ifourtechnolab.com/blog/a-step-by-step-guide-on-using-redux-toolkit-with-react

3. Tích hợp Redux vào react

Để sử dụng Redux trong React, bạn cần các bước sau:

Bước 1: Cài đặt Redux và React-Redux

Cài đặt Redux và React-Redux để bắt đầu tích hợp vào dự án React:

				
					npm install @reduxjs/toolkit react-redux

				
			

Bước 2: Tạo Store

Tạo một store để quản lý trạng thái toàn cục của ứng dụng:

				
					import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './features/counter/counterSlice';

const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
});

export default store;

				
			

Bước 3: Tạo Slice

Redux Toolkit giới thiệu khái niệm “slice” giúp quản lý các phần trạng thái của ứng dụng dễ dàng hơn.

				
					import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
  },
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;

				
			

Bước 4: Kết nối Redux với React

Sử dụng Provider từ react-redux để kết nối store với các component React.

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

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

				
			

4. Sử dụng Redux trong các Component React

Để lấy dữ liệu từ Redux store hoặc gửi action, bạn sử dụng các hook useSelector useDispatch.

useSelector – Lấy trạng thái từ Store

				
					import { useSelector } from 'react-redux';

const CounterDisplay = () => {
  const count = useSelector((state) => state.counter);
  return <div>Count: {count}</div>;
};

				
			

useDispatch – Gửi Action để thay đổi trạng thái

				
					import { useDispatch } from 'react-redux';
import { increment, decrement } from './features/counter/counterSlice';

const CounterButtons = () => {
  const dispatch = useDispatch();
  return (
    <div>
      <button onClick={() => dispatch(increment())}>Increase</button>
      <button onClick={() => dispatch(decrement())}>Decrease</button>
    </div>
  );
};

				
			

5. Lợi ích của Redux

6. hạn chế của Redux

7. Khi nào nên và không nên sử dụng Redux

8. Kết luận

Redux là một công cụ hữu ích để quản lý trạng thái phức tạp trong ứng dụng React. Với Redux Toolkit, bạn có thể làm việc với Redux một cách dễ dàng hơn và tránh được nhiều vấn đề tiềm ẩn. Nếu bạn mới bắt đầu, hãy thử tạo một ứng dụng đơn giản với Redux để hiểu rõ hơn về cách hoạt động của nó.

Ở trên là 1 số chia sẻ về redux trong react của mình, vì đây đa phần là kiến thức mình tự học nên có thể bài viết này có thể có nhiều chỗ chưa chính xác và thiếu sót, mong mọi người góp ý ở dưới phần bình luận để bài viết thêm hoàn chỉnh và xây dựng một cộng đồng nơi mọi người có thể chia sẻ kinh nghiệm lập trình cho nhau 😘.

Bài viết liên quan

Building a High-Converting Shopify Product Page: Design, Copy, and Psychology

Your Shopify product page is the digital storefront where potential customers decide whether to make a purchase. It’s…

ByByAuto Mation Dec 30, 2025

The Impact of Page Speed on Conversions: Optimizing Your Shopify Store for a Faster User Experience

In the fast-paced world of e-commerce, every second counts. A slow-loading Shopify store can be the difference between…

ByByAuto Mation Dec 29, 2025

Shopify Order Routing: How to Ship From Multiple Warehouses Efficiently

In today’s competitive e-commerce landscape, efficient order fulfillment is crucial for customer satisfaction and business success. For Shopify…

ByByAuto Mation Dec 29, 2025

Shopify SEO Beyond Keywords: Optimizing for Voice Search and Visual Discovery

In the ever-evolving landscape of e-commerce, staying ahead of the curve is paramount for Shopify store owners. While…

ByByAuto Mation Dec 29, 2025

Subscribe
Thông báo của
guest

0 Comments
Cũ nhất
Mới nhất Được bình chọn nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận

0
Rất mong nhận được suy nghĩ của bạn, vui lòng bình luận.x
()
x