{"id":13672914,"url":"https://github.com/xd-tayde/easy-redux-react","last_synced_at":"2025-04-10T02:35:20.039Z","repository":{"id":40948323,"uuid":"184173167","full_name":"xd-tayde/easy-redux-react","owner":"xd-tayde","description":"the helper to make use redux easier with react.","archived":false,"fork":false,"pushed_at":"2023-01-03T22:49:26.000Z","size":2010,"stargazers_count":3,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T18:08:27.783Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xd-tayde.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-30T01:56:21.000Z","updated_at":"2023-03-05T01:43:53.000Z","dependencies_parsed_at":"2023-02-01T12:16:57.758Z","dependency_job_id":null,"html_url":"https://github.com/xd-tayde/easy-redux-react","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xd-tayde%2Feasy-redux-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xd-tayde%2Feasy-redux-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xd-tayde%2Feasy-redux-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xd-tayde%2Feasy-redux-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xd-tayde","download_url":"https://codeload.github.com/xd-tayde/easy-redux-react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144462,"owners_count":21054934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:56.973Z","updated_at":"2025-04-10T02:35:20.021Z","avatar_url":"https://github.com/xd-tayde.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"## Easy-Redux-React\n\n该插件封装了 react-redux 的 api, 集中化管理 store, action 与 reducer, 简化了使用,极小的接入成本和最简单的使用方式，即插即用。\n\n### Usage\n\n```js\nimport EasyReduxReact from 'easy-redux-react'\nimport ReactDOM from 'react-dom'\n\n// redux-store\nconst _ins = new EasyReduxReact({\n\treduxConfig: {\n\t    // key\n\t    list: {\n\t        // initState\n\t        initValue: [],\n\t        // actions map\n\t        actions: {\n\t            // action name: reducer\n\t            ADD_LIST: (state, data) =\u003e state.concat(data.payload),\n               \tREMOVE_LIST: (state, data) =\u003e {\n                    const _list = [...state]\n                    _list.splice(data.payload, 1)\n                    return _list\n               },\t        \n           },\n\t    },\n\t},\n})\n\nconst App = () =\u003e {\n\t// 可以直接使用连接后的 state 与 action\n\tconst { list, addList, removeList } = props\n\n\treturn (\n\t\t \u003cdiv id=\"App\"\u003eeasy-redux-react\u003c/div\u003e\n\t)\n}\n\nconst ReduxApp = _ins.connectTo(App)\nconst Provider = _ins.getProvider()\n \nReactDOM.render(\n\t\u003cProvider\u003e\n        \u003cReduxApp /\u003e\n    \u003c/Provider\u003e,\n\tmountNode,\n)\n```\n\n#### 1. 初始化实例\n\n```js\nnew EasyReduxReact({\n    // redux config include state - action - reducer\n    reduxConfig: {\n    \t[stateKey: string]: {\n\t        initValue: any,\n\t        actions: {\n\t            [reducerName: string]: (state: any, data: { payload: number, type: string }) =\u003e any,\n\t        },\n\t    }\n    }\n    \n    // use it to init store with the ssr pre-fetch data\n    hydrateData?: object\n    \n    // check the async reducer res, if true, will trigger the success action\n    checkRes?: (res: any) =\u003e boolean\n    \n    // handle the async reducer res\n    handleRes?: (res: any) =\u003e any\n})\n```\n\n#### 1. reduxConfig\n\nredux 的核心配置，包含 state - action - reducer，例如：\n\n```js\nconst reduxConfig = {\n\t// 存储于 store 中的 key 值\n\tuserInfo: {\n\t\t// 该值的初始化状态\n\t\tinitValue: null,\n\t\t// 与该值关联的所有 action\n\t\tactions: {\n\t\t\t// action name:  reducer\n\t\t\t// 触发 action 时传入的数据固定保存在 data.payload 中\n\t\t\t// data = { type: 'SET_USER_INFO', payload: {} }\n\t\t\t// state 为该数据原先的值\n\t\t\tSET_USER_INFO: (state, data) =\u003e data.payload,\n\t\t\t\n\t\t\t// 清空 userInfo \n\t\t\tEMPTY_USER_INFO: () =\u003e null,\n\t\t}\n\t}\n}\n```\n\n### 连接组件\n\n```js\n// 连接用户信息\n_ins.connectTo(Component, stateKeys?: string[], dispatchMap?: object)\n```\n\n#### 1. stateKeys\n\n该组件中需要注入的 state key，例如:\n\n```js\n// store 为 { a: 1, b: 2 }\n// A组件中只需要引入数据a， 则:\n\nconst stateKeys = ['a']\n_ins.connectTo(ComponentA, stateKeys)\n\n```\n\n**不传该值时，默认将 store 中所有数据均注入到组件中 **\n\n#### 2. dispatchMap\n\n连接后，组件中 props 上的方法与 action 的映射关系，例如：\n\n```js\n\ninterface IDispatchMap {\n    [actionName: string]: string | {\n        fetch: (...params: any) =\u003e Promise\u003cany\u003e,\n        success: string,\n        error?: string,\n    }\n}\n\nconst dispatchMap: IDispatchMap = {\n\t// 异步触发\n\tsetUserInfo: {\n\t\t// 通过执行该异步函数获取 userInfo\n\t\t// 该方法必须返回一个 promise\n\t\tfetch: getUserInfoWithAjax,\n\t\t// 获取成功时，触发成功 Action\n\t\tsuccess: 'SET_USER_INFO',\n\t\t// 获取失败时，触发错误的 Action\n\t\terror: 'FETCH_ERROR',\n\t},\n\t// 同步触发\n\temptyUserInfo: 'EMPTY_USER_INFO',\n}\n```\n\n**不传该值时，默认将连接的数据的所有 action 注入 props 中，方法名为驼峰的形式，例如: SET_USER_INFO ---\u003e props.setUserInfo **\n\n#### 3. 使用\n\n连接后，即可以直接在 Component 中进行使用\n\n```js\n// 在 Component 中即可以使用 \n// 获取全局 store 中的属性\nprops.userInfo\n\n// 同时自行添加了对应的 dispatch 方法\n// 方法名: Action 名字转成 驼峰\n// eg. SET_USER_INFO\nprops.setUserInfo()\n\n// 如果是异步\n// 调用\nprops.setUserInfo(xxx).then((data) =\u003e {\n    // 异步请求成功: data\n    // 会自动触发 action 并存储到 redux-store 中\n}).catch(err =\u003e {\n    // 捕获请求失败\n})\n```\n\n### 暴露原生 API，可任意地方自行引用后调用\n\n```js\nconst { dispatch, getState, subscribe } = _ins.getStore()\n\n// 手动触发\ndispatch({\n    type: 'SET_USER_INFO',\n    payload: {\n        name: 'dongdong'\n    }\n})\n\n// 获取全局数据\ngetState()\n\n// 订阅数据变化\nsubscribe(() =\u003e {\n    // store 发生变化时触发\n})\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxd-tayde%2Feasy-redux-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxd-tayde%2Feasy-redux-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxd-tayde%2Feasy-redux-react/lists"}