npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cosmos-redux

v1.2.17

Published

[![npm version](https://img.shields.io/npm/v/cosmos-redux.svg)](https://www.npmjs.com/package/cosmos-redux) [![npm downloads](https://img.shields.io/npm/dm/cosmos-redux.svg)](https://www.npmjs.com/package/cosmos-redux)

Downloads

17

Readme

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