目录
一、redux介绍
二、安装
三、基本实现步骤
3.1 创建Action Types
3.2 创建counterAction
3.3 创建counterReducer
3.4 结合所有Reducer
3.5 创建store
3.6 入口文件中提供store
3.7 在组件中的使用
3.8 使用thunk实现异步支持
3.8.1 安装
3.8.2 在counterAction中添加异步操作
3.8.3 在store文件中添加异步支持
3.8.4 在组件中进行异步操作
3.9 函数式组件使用hooks代替connect
Redux 基于三个基本原则:
单一数据源:整个应用的状态存储在一个对象树中
状态是只读的:唯一改变状态的方法是触发 action
使用纯函数执行修改:reducer 是纯函数,接收旧 state 和 action,返回新 state
npm install redux react-redux
// actionTypes.js
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";
// store/actions/counterAction.js
import { INCREMENT, DECREMENT } from "../../utils/actionTypes";
export const increment = () => ({
type: INCREMENT,
});
export const decrement = () => ({
type: DECREMENT,
});
// store/reducers/counterReducer.js
import { INCREMENT, DECREMENT } from "../../utils/actionTypes";
const initialState = {
count: 0,
};
export default function 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;
}
}
// store/reducers/index.js
import { combineReducers } from "redux";
import counterReducer from "./counterReducer";
export default combineReducers({
counter: counterReducer,
});
// store/index.js
import { createStore } from "redux";
import reducers from "./reducers";
const store = createStore(reducers);
export default store;
// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import store from "./store";
import { Provider } from "react-redux";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
);
import React from "react";
import { connect } from "react-redux";
import { increment, decrement } from "./store/actions/counterAction";
function App(props) {
const { count, increment, decrement } = props;
return (
数值:{count}
);
}
const mapStateToProps = (state) => ({
count: state.counter.count, // store/reducers/index.js中的counter映射
});
const mapDispatchToProps = {
increment,
decrement,
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
npm install redux-thunk
// store/actions/counterAction.js
import { INCREMENT, DECREMENT } from "../../utils/actionTypes";
export const increment = () => ({
type: INCREMENT,
});
export const decrement = () => ({
type: DECREMENT,
});
export const fetchIncrement = (timeout) => {
return (dispatch) => {
setTimeout(() => {
dispatch(increment());
}, timeout);
};
};
// store.js
import { createStore, applyMiddleware } from "redux";
import { thunk } from "redux-thunk";
import reducers from "./reducers";
const store = createStore(reducers, applyMiddleware(thunk));
export default store;
import React from "react";
import { connect } from "react-redux";
import {
increment,
decrement,
fetchIncrement,
} from "./store/actions/counterAction";
function App(props) {
const { count, increment, decrement, fetchIncrement } = props;
return (
数值:{count}
);
}
const mapStateToProps = (state) => ({
count: state.counter.count, // store/reducers/index.js中的counter映射
});
const mapDispatchToProps = {
increment,
decrement,
fetchIncrement,
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
// App.js
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import {
increment,
decrement,
fetchIncrement,
} from "./store/actions/counterAction";
export default function App(props) {
const { count } = useSelector((state) => state.counter); // store/reducers/index.js中的counter映射
const dispatch = useDispatch();
return (
数值:{count}
);
}