From 6dac47bdec5aecb591221182208bcf8c6b426a21 Mon Sep 17 00:00:00 2001 From: chongmingkj_xwq Date: Tue, 27 Feb 2024 08:42:34 +0800 Subject: [PATCH] =?UTF-8?q?[Issues:=20#I92LZH]=20=E6=B7=BB=E5=8A=A0redux-a?= =?UTF-8?q?ctions=E6=8C=87=E5=AF=BC=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1224/redux-actions.md | 266 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 1224/redux-actions.md diff --git a/1224/redux-actions.md b/1224/redux-actions.md new file mode 100644 index 00000000..f8bf408f --- /dev/null +++ b/1224/redux-actions.md @@ -0,0 +1,266 @@ +> 模板版本:v0.1.3 + +

+

redux-actions

+

+

+ + Supported platforms + + + License + + +

+ +> [!tip] [Github 地址](https://github.com/redux-utilities/redux-actions) + +## 安装与使用 + + + +#### **npm** +```bash +npm install redux-actions@^2.6.5 +``` + +#### **yarn** + +```bash +yarn add redux-actions@^2.6.5 +``` + + + +下面的代码展示了这个库的基本使用场景: + +1.定义state的类型 + +// LoginType.ts +```ts +export interface IAppUser{ + id:string, + name:string +} +``` +2.定义action + +// loginAction.ts +```ts +import { IAppUser } from './loginType'; +import { createActions } from 'redux-actions'; + +export const LOGIN_ACTION = { + LOGIN: 'login', + LOGOUT: 'logout', + CHANGENAME:'changName' +}; + +export default createActions({ + [LOGIN_ACTION.LOGIN]: (appUser: IAppUser) => appUser, + [LOGIN_ACTION.LOGOUT]: () => null, + [LOGIN_ACTION.CHANGENAME]:(appUser: IAppUser) => appUser, +}); +``` + +3.定义reducer + +// loginReducer.ts + +```ts +import { LOGIN_ACTION } from './loginAction'; +import { Action, handleActions as createReducers, combineActions } from 'redux-actions'; +import { IAppUser } from './loginType'; + +const defaultState = { + // defautl value 不能为null + appUser: {}, +}; + +export default createReducers( + { + // payload 参数名固定,类型推导 + [LOGIN_ACTION.LOGIN]: (state, { payload }: Action) => { + console.log('createReducers LOGIN_ACTION.LOGIN',JSON.stringify(payload)) + return { + ...state, + appUser:payload, + } + }, + // 异常处理 + [LOGIN_ACTION.LOGOUT]: { + next(state) { + console.log('createReducers LOGIN_ACTION.LOGOUT',JSON.stringify({})) + return { + ...state, + appUser:{} + }; + }, + throw(state) { + return state; + }, + }, + [`${combineActions(LOGIN_ACTION.LOGIN, LOGIN_ACTION.CHANGENAME)}`]: (state, { payload }: Action) =>{ + console.log("createReducers combineActions", JSON.stringify(state.appUser), 'action'+ JSON.stringify(payload)) + return { + ...state, + appUser:payload + } + } + }, + defaultState +); +``` + +4.定义store + +// loginStore.ts +```ts +import { legacy_createStore as createStore, combineReducers } from 'redux'; +import loginReducer from './loginReducer'; + +export const rootReducer = combineReducers({ + login: loginReducer +}); + +const store = createStore(rootReducer); + +export type RootState = ReturnType; +export type AppDispatch = typeof store.dispatch; +export default store; +``` + +5.通过connector将state和action挂载到组件 + +// loginDemo.tsx +```tsx +import { connect, ConnectedProps } from 'react-redux'; +import { Button,View ,Text, StyleSheet} from 'react-native'; +import { RootState } from './loginStore'; +import actions, { LOGIN_ACTION } from './loginAction'; + +const mapState = (state: RootState) => ({ + appUser: state.login.appUser, +}); + +const mapDispatch = { + onLogin: actions[LOGIN_ACTION.LOGIN], + onLogout: actions[LOGIN_ACTION.LOGOUT], + onChangeName:actions[LOGIN_ACTION.CHANGENAME] +}; + +const connector = connect(mapState, mapDispatch); +type PropsFromRedux = ConnectedProps; + +const Login = (props: PropsFromRedux) => { + return ( + + + appUser:{JSON.stringify(props.appUser)} + + +