1.2.17 • Published 2 years ago

cosmos-redux v1.2.17

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Cosmos Redux

npm version npm downloads

对redux toolkit的再次封装

Installation

cosmos-redux is available as a package on NPM for use with a module bundler or in a Node application:

# NPM
npm install cosmos-redux --save

# Yarn
yarn add cosmos-redux

usage

创建redux应用

// utils/redux.ts
import { configReduxApp } from 'cosmos-redux';

/**
 * 此处同 @reduxjs/toolkit 的 configureStore 的 options
 * 唯一不同点在于 reducer 属性只能是 ReducersMapObject 类型
 */
const options = {};

export const app = configReduxApp(options).complete();

export const store = app.getStore();
// app.tsx
import { Provider } from 'cosmos-redux';
import { store } from 'utils/redux';

// render
<Provider store={store}>
  <App />
</Provider>

使用插件

// utils/redux.ts
import { configReduxApp, thunkLoadingPlugin } from 'cosmos-redux';

/**
 * 此处同 @reduxjs/toolkit 的 configureStore 的 options
 * 唯一不同点在于 reducer 属性只能是 ReducersMapObject 类型
 */
const options = {};

export const app = configReduxApp(options);

app.addPlugin(thunkLoadingPlugin).complete();

创建model

创建 model 时,参数同 @reduxjs/toolkitcreateSlice

区别在于

  • 1、增加了两个参数,thunksthunksBuilder
  • 2、extraReducers 只能使用函数方式

    thunks 是一个 ThunkCreatorMap 对象,传入后会自动调用 createAsyncThunk 为每一个 key 创建一个对应的 AsyncThunk,得到一个 AsyncThunkMap 对象

    thunksBuilder 是一个函数,作用同 extraReducers,区别在于能从参数中能获取到创建好的 AsyncThunkMap 对象

    model 是一个对象,其扩展了 createSlice 方法创建的对象,增加了 thunks 和 selectors 两个属性,thunks 为 AsyncThunkMap 对象,selectors 为属性的选择函数的映射,用于使用 useSelector 选择属性。

// model.ts
import { app } from 'utils/redux';

const model = app.createModel({
  name: 'todo',
  initialState: {
    todoList: []
  },
  reducers: {},
  thunks: {
    getTodoList: async () => {
      const list = await fetch('/api/todoList');
      return list;
    }
  },
  thunksBuilder: (thunks, builder) => {
    builder.addCase(thunks.getTodoList.fulfilled, (state, { payload }) => {
      state.todoList = payload;
    })
  }
});

export const { actions, thunks, selectors } = model;

使用 model

  • useActions

useActions 会对传入进来的 ActionCreatorMap 对象调用 redux 中的 bindActionCreators 方法,自动绑定 dispath,得到调用 dispatch 的函数Map。

  • useThunkLoading

在添加了 thunkLoadingPlugin 插件后,useThunkLoading 会自动根据传入的 thunkCreator 获取到当前的 pending 状态。

  • selectors

selectors 是一个选择属性的函数的集合

import React, { useEffect } from 'react';
import { useActions, useThunkLoading, useSelector } from 'cosmos-redux';
import { Table } from 'antd';

import { actions, thunks, selectors } from './model';

const TodoList = () => {
  const actions = useActions(actions, thunks);
  const loading = useThunkLoading(thunks.getTodoList);
  const todoList = useSelector(selectors.todoList);

  useEffect(() => {
    actions.getTodoList();
  }, []);

  return <Table loading={loading} dataSource={todoList} />
}

Documentation

1.2.17

2 years ago

1.2.16

3 years ago

1.2.15

3 years ago

1.2.13

3 years ago

1.2.14

3 years ago

1.2.8

3 years ago

1.2.9

3 years ago

1.2.12

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago