当前位置:首页 > React

react如何集成redux

2026-02-11 23:13:39React

集成 Redux 到 React 的步骤

安装依赖
确保项目已安装 reduxreact-redux。通过以下命令安装:

npm install redux react-redux

创建 Redux Store
在项目中创建 store.js 文件,初始化 Redux store 并定义 reducer。

import { createStore } from 'redux';

// 初始状态和 reducer
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;

使用 Provider 包裹根组件
在应用的入口文件(如 index.js)中,用 react-reduxProvider 包裹根组件,并传入 store。

react如何集成redux

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

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

连接组件到 Redux
在需要访问状态的组件中,使用 connect 函数将组件与 Redux store 连接。

import React from 'react';
import { connect } from 'react-redux';

const Counter = ({ count, increment, decrement }) => (
  <div>
    <p>Count: {count}</p>
    <button onClick={increment}>Increment</button>
    <button onClick={decrement}>Decrement</button>
  </div>
);

const mapStateToProps = (state) => ({
  count: state.count,
});

const mapDispatchToProps = (dispatch) => ({
  increment: () => dispatch({ type: 'INCREMENT' }),
  decrement: () => dispatch({ type: 'DECREMENT' }),
});

export default connect(mapStateToProps, mapDispatchToProps)(Counter);

可选优化:使用 Redux Toolkit

对于更复杂的场景,推荐使用 @reduxjs/toolkit 简化 Redux 逻辑。

react如何集成redux

安装 Redux Toolkit

npm install @reduxjs/toolkit

重构 Store 和 Reducer
使用 createSlice 自动生成 action creators 和 reducer。

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

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

export const { increment, decrement } = counterSlice.actions;
const store = configureStore({ reducer: counterSlice.reducer });
export default store;

在组件中使用生成的 Actions
通过 useDispatchuseSelector 钩子简化代码。

import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { increment, decrement } from './store';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => dispatch(increment())}>Increment</button>
      <button onClick={() => dispatch(decrement())}>Decrement</button>
    </div>
  );
};

export default Counter;

标签: reactredux
分享给朋友:

相关文章

react native 如何

react native 如何

React Native 开发基础 React Native 是一个用于构建跨平台移动应用的框架,允许开发者使用 JavaScript 和 React 编写代码,同时生成原生 iOS 和 Androi…

react 如何执行

react 如何执行

安装 Node.js 和 npm React 开发需要 Node.js 环境,因为它提供了 npm(或 yarn)包管理工具。从 Node.js 官网 下载并安装最新 LTS 版本。安装完成后,在终端…

react如何销毁

react如何销毁

React 组件销毁的机制 在 React 中,组件的销毁通常由 React 的生命周期管理。当组件从 DOM 中移除时,React 会自动触发销毁相关的生命周期方法。以下是关键点: 组件的销毁通常…

react性能如何

react性能如何

React 性能特点 React 通过虚拟 DOM(Virtual DOM)和高效的 diff 算法优化性能。虚拟 DOM 减少了直接操作真实 DOM 的开销,仅在必要时更新变化的部分。React 的…

如何下载react

如何下载react

下载 React 的方法 通过 npm 或 yarn 安装 React 可以通过包管理器 npm 或 yarn 安装。确保已安装 Node.js(包含 npm),然后在项目目录中运行以下命令:…

如何创建react

如何创建react

创建React项目的步骤 使用Create React App工具快速搭建React项目。确保已安装Node.js(版本需≥14.0.0)和npm(版本需≥5.6)。 打开终端或命令行工具,运行以下…