react如何使用redux
使用 Redux 在 React 中的基本步骤
安装依赖
确保项目已安装 redux 和 react-redux:
npm install redux react-redux
创建 Redux Store
在 store.js 中定义 store 并关联 reducer:
import { createStore } from 'redux';
import rootReducer from './reducers';
const store = createStore(rootReducer);
export default store;
定义 Reducer
在 reducers/index.js 中组合多个 reducer:
import { combineReducers } from 'redux';
import counterReducer from './counterReducer';
const rootReducer = combineReducers({
counter: counterReducer,
});
export default rootReducer;
创建 Action
在 actions/counterActions.js 中定义 action 类型和生成函数:
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const increment = () => ({ type: INCREMENT });
export const decrement = () => ({ type: DECREMENT });
实现 Reducer 逻辑
在 reducers/counterReducer.js 中处理 action:
import { INCREMENT, DECREMENT } from '../actions/counterActions';
const initialState = { count: 0 };
const counterReducer = (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;
}
};
export default counterReducer;
连接 React 组件
通过 Provider 传递 store 并在组件中使用 connect:
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')
);
在组件中访问 Redux
使用 connect 或 Hooks(如 useSelector 和 useDispatch):
import React from 'react';
import { connect } from 'react-redux';
import { increment, decrement } from './actions/counterActions';
const Counter = ({ count, increment, decrement }) => (
<div>
<button onClick={decrement}>-</button>
<span>{count}</span>
<button onClick={increment}>+</button>
</div>
);
const mapStateToProps = (state) => ({ count: state.counter.count });
const mapDispatchToProps = { increment, decrement };
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
使用 Redux Hooks 的替代方案
函数组件中使用 Hooks
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './actions/counterActions';
const Counter = () => {
const count = useSelector((state) => state.counter.count);
const dispatch = useDispatch();
return (
<div>
<button onClick={() => dispatch(decrement())}>-</button>
<span>{count}</span>
<button onClick={() => dispatch(increment())}>+</button>
</div>
);
};
export default Counter;
异步操作处理(Redux Thunk)
安装中间件
npm install redux-thunk
配置 Store
修改 store.js 以支持 Thunk:
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const store = createStore(rootReducer, applyMiddleware(thunk));
export default store;
定义异步 Action
在 actions/counterActions.js 中:
export const asyncIncrement = () => (dispatch) => {
setTimeout(() => {
dispatch(increment());
}, 1000);
};
在组件中调用异步 Action
<button onClick={() => dispatch(asyncIncrement())}>Async +</button>





