1925 字
10 分钟

React 状态管理

React 状态管理#

状态管理是React应用开发中的一个重要部分,用于管理应用中的数据和状态。随着应用规模的增长,状态管理变得越来越复杂,需要选择合适的状态管理方案。

1. 组件状态#

对于简单的应用,使用React内置的状态管理功能(useState和useReducer)就足够了。

useState#

useState是最基本的状态管理Hook,用于在函数组件中添加状态。

import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(count - 1)}>Decrement</button>
</div>
);
}
export default Counter;

useReducer#

useReducer是一个更强大的状态管理Hook,适用于复杂的状态逻辑。

import React, { useReducer } from 'react';
// 定义reducer函数
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
case 'reset':
return { count: 0 };
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => dispatch({ type: 'increment' })}>Increment</button>
<button onClick={() => dispatch({ type: 'decrement' })}>Decrement</button>
<button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
</div>
);
}
export default Counter;

2. Context API#

Context API是React内置的一种状态管理方案,用于在组件树中共享状态,避免props drilling问题。

创建Context#

import React, { createContext, useState, useContext } from 'react';
// 创建Context
const ThemeContext = createContext();
// 创建Provider组件
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState('light');
const toggleTheme = () => {
setTheme(theme === 'light' ? 'dark' : 'light');
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
}
// 创建自定义Hook,方便使用Context
export function useTheme() {
const context = useContext(ThemeContext);
if (!context) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
}

使用Context#

import React from 'react';
import { ThemeProvider, useTheme } from './ThemeContext';
function ThemedButton() {
const { theme, toggleTheme } = useTheme();
return (
<button
onClick={toggleTheme}
style={{
background: theme === 'light' ? '#fff' : '#333',
color: theme === 'light' ? '#333' : '#fff',
padding: '10px',
border: '1px solid #ccc'
}}
>
Toggle Theme ({theme})
</button>
);
}
function App() {
return (
<ThemeProvider>
<div>
<h1>Theme Example</h1>
<ThemedButton />
</div>
</ThemeProvider>
);
}
export default App;

3. Redux#

Redux是一个流行的状态管理库,用于管理复杂的应用状态。它提供了一个集中式的状态存储,使得状态管理更加可预测和可维护。

安装Redux#

Terminal window
# 使用npm安装
npm install @reduxjs/toolkit react-redux
# 使用yarn安装
yarn add @reduxjs/toolkit react-redux
# 使用pnpm安装
pnpm add @reduxjs/toolkit react-redux

创建Redux Store#

src/app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});

创建Slice#

src/features/counter/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
export const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
// Redux Toolkit允许我们在reducers中直接修改state,它会自动处理不可变性
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
// 导出action creators
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
// 导出reducer
export default counterSlice.reducer;

配置Provider#

src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { store } from './app/store';
import { Provider } from 'react-redux';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);

使用Redux#

src/App.js
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement, incrementByAmount } from './features/counter/counterSlice';
function App() {
// 使用useSelector获取state
const count = useSelector((state) => state.counter.value);
// 使用useDispatch获取dispatch函数
const dispatch = useDispatch();
return (
<div>
<h1>Redux Counter Example</h1>
<p>Count: {count}</p>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
<button onClick={() => dispatch(incrementByAmount(5))}>Increment by 5</button>
</div>
);
}
export default App;

4. MobX#

MobX是另一个流行的状态管理库,它使用可观察对象来管理状态,提供了更简洁的API。

安装MobX#

Terminal window
# 使用npm安装
npm install mobx mobx-react-lite
# 使用yarn安装
yarn add mobx mobx-react-lite
# 使用pnpm安装
pnpm add mobx mobx-react-lite

创建Store#

src/stores/counterStore.js
import { makeAutoObservable } from 'mobx';
class CounterStore {
count = 0;
constructor() {
// 自动将类的属性和方法转换为可观察的
makeAutoObservable(this);
}
increment() {
this.count += 1;
}
decrement() {
this.count -= 1;
}
incrementByAmount(amount) {
this.count += amount;
}
}
// 导出单例
export const counterStore = new CounterStore();

使用MobX#

src/App.js
import React from 'react';
import { observer } from 'mobx-react-lite';
import { counterStore } from './stores/counterStore';
// 使用observer高阶组件包装组件,使其响应状态变化
const App = observer(() => {
return (
<div>
<h1>MobX Counter Example</h1>
<p>Count: {counterStore.count}</p>
<button onClick={() => counterStore.increment()}>Increment</button>
<button onClick={() => counterStore.decrement()}>Decrement</button>
<button onClick={() => counterStore.incrementByAmount(5)}>Increment by 5</button>
</div>
);
});
export default App;

5. Zustand#

Zustand是一个轻量级的状态管理库,它使用Hook来管理状态,提供了简洁的API和良好的性能。

安装Zustand#

Terminal window
# 使用npm安装
npm install zustand
# 使用yarn安装
yarn add zustand
# 使用pnpm安装
pnpm add zustand

创建Store#

src/stores/counterStore.js
import create from 'zustand';
export const useCounterStore = create((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
incrementByAmount: (amount) => set((state) => ({ count: state.count + amount })),
}));

使用Zustand#

src/App.js
import React from 'react';
import { useCounterStore } from './stores/counterStore';
function App() {
// 使用useCounterStore Hook获取状态和操作
const count = useCounterStore((state) => state.count);
const increment = useCounterStore((state) => state.increment);
const decrement = useCounterStore((state) => state.decrement);
const incrementByAmount = useCounterStore((state) => state.incrementByAmount);
return (
<div>
<h1>Zustand Counter Example</h1>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
<button onClick={() => incrementByAmount(5)}>Increment by 5</button>
</div>
);
}
export default App;

6. 状态管理方案比较#

方案适用场景优点缺点
组件状态 (useState/useReducer)简单组件,局部状态简单易用,集成在React中不适合跨组件共享状态
Context API跨组件共享状态,中等复杂度集成在React中,避免props drilling可能导致不必要的重渲染
Redux复杂应用,全局状态管理可预测性强,工具生态丰富代码量较大,学习曲线较陡
MobX复杂应用,全局状态管理代码简洁,响应式编程可能导致不可预测的状态变化
Zustand中小型应用,全局状态管理轻量级,API简洁,性能好生态相对较小

7. 最佳实践#

  1. 根据应用规模选择合适的状态管理方案

    • 小型应用:使用组件状态或Context API
    • 中型应用:使用Context API或Zustand
    • 大型应用:使用Redux或MobX
  2. 合理设计状态结构

    • 保持状态结构扁平,避免深层嵌套
    • 按功能模块组织状态
    • 只存储必要的状态,避免存储派生数据
  3. 优化性能

    • 使用useCallback和useMemo缓存函数和计算结果
    • 对于Context API,考虑使用多个Context来避免不必要的重渲染
    • 对于Redux,使用selectors来优化状态获取
  4. 保持状态逻辑清晰

    • 将状态逻辑与UI组件分离
    • 使用自定义Hook封装状态逻辑
    • 编写清晰的注释和文档
  5. 测试状态管理

    • 测试reducers和actions
    • 测试状态更新是否正确
    • 测试组件在不同状态下的行为

练习#

  1. 使用useState创建一个简单的计数器组件。
  2. 使用useReducer创建一个更复杂的计数器组件,支持增加、减少和重置操作。
  3. 使用Context API创建一个主题管理系统,支持切换明/暗主题。
  4. 使用Redux创建一个待办事项应用,支持添加、删除和标记完成功能。
  5. 使用Zustand创建一个简单的全局状态管理应用。

总结#

在本章节中,我们学习了React中的状态管理方法和最佳实践,包括:

  • 组件状态:使用useState和useReducer管理局部状态
  • Context API:在组件树中共享状态,避免props drilling
  • Redux:使用集中式存储管理复杂的应用状态
  • MobX:使用可观察对象管理状态,提供简洁的API
  • Zustand:使用Hook管理状态,轻量级且性能好
  • 状态管理方案的比较和选择
  • 状态管理的最佳实践

选择合适的状态管理方案对于React应用的开发和维护非常重要。根据应用的规模和复杂度,选择最适合的状态管理方案,可以提高开发效率,改善应用性能,使代码更加可维护。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

React 状态管理
https://firefly.cuteleaf.cn/posts/react/07-react-state-management/
作者
Lireal
发布于
2026-01-21
许可协议
CC BY-NC-SA 4.0

目录