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

umi-plugin-redux-toolkit

v2.3.1

Published

[中文文档](README_zhCN.md)

Downloads

76

Readme

umi-plugin-redux-toolkit

中文文档

The plugin of umi@4 uses @reduxjs/toolkit.

Install and use

If you want to use plugin, you must install first.

yarn add umi-plugin-redux-toolkit @reduxjs/toolkit
// or
npm install umi-plugin-redux-toolkit @reduxjs/toolkit

Finish installed, you may enable the plugin.

Configuration

  • ignoreOptions { object } : Configure ignored options, refer to
    https://redux-toolkit.js.org/api/serializabilityMiddleware,
    When the object in redux may be created from new Class, or other non-serializable values,
    there will be a warning when the action is obtained or called, and this option can be configured to ignore the warning.

    • ignoreOptions.ignoredPaths { Array<string> } : Ignore value.
    • ignoreOptions.ignoredActions { Array<string> } : Ignored actions.
  • modelName { string } : Customize the name of the model folder and ignore the singular configuration after configuration.

  • singular { boolean } : Whether the directory is singular.

  • esModule { boolean } : Import using the es6 module method.

  • ignore { string | Array } : Ignored model files. (Refer to the ignore configuration of glob)

  • asyncLoadReducers { boolean } : Enable the function of asynchronously injecting reducers.
    (Need to manually mount on the component, it will be more troublesome, so it is not recommended).

How to configure

// .umirc.js or umi.config.js
import { defineConfig } from 'umi';

export default {
  // Configuration of umi-plugin-redux-toolkit
  reduxToolkit: {
    esModule: true
  }
};

How to use

Create the models folder, create a ts or js file under the folder, and export the slice created by createSlice, or the configuration of createSlice.
Export slice Reference, Export the configuration of createSlice Reference.

import { createSlice } from '@redux/toolkit';

const slice = createSlice({
  name: 'sliceName',
  initialState: { value: 0 },
  reducers: {
    addValue(state, action) {
      state.value++;
    }
  }
});

const { addValue } = slice.actions;

export default slice;

When exporting the configuration of createSlice, if the reduce key is created by createAction, the namespace will be automatically removed. E.g:

import { createAction } from '@redux/toolkit';

const action = createAction('sliceName/action');

export default {
  name: 'sliceName',
  initialState: { value: 0 },
  reducers: {
    // will automatically change index/action into action
    [action](state, action) {
      return state;
    }
  }
};

Now also supports exporting APIs created by RTK RTKQuery. E.g:

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

const api = createApi({
  reducerPath: 'api',
  baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  endpoints(builder) {
    return {
      getData: builder.query({
        query: (q) => q
      })
    };
  }
});

export const { useGetDataQuery } = api;
export default api;

Exporting listenerMiddleware is now also supported. E.g:

import { createListenerMiddleware } from '@reduxjs/toolkit';
import { setAction } from './actions';

const listenerMiddleware = createListenerMiddleware();

listenerMiddleware.startListening({
  actionCreator: setAction,
  effect(action, listenerApi) {
    console.log('Listener: setAction');
  }
});

export default listenerMiddleware;

Initial value

Export in app.js

export const reduxToolkit = {
  initialState: {},    // Initialize the value of redux
  ignoreOptions: {},   // Same as the ignoreOptions configuration above, it will be merged
  warnAfter: 800,      // If the check time of immutableCheck and serializableCheck exceeds 32ms, there will be a warning. Modify the warning time
  reducers: {},        // Custom add reducers
  middlewares: [],     // Custom add middlewares
  treatStore(store) {} // Allows you to perform other processing on the store, such as mounting some monitoring methods
};

// or

export function reduxToolkit() {
  return {
    initialState: () => ({}), // It can also be a function to initialize the value of redux
    ignoreOptions: {},        // Same as the ignoreOptions configuration above, it will be merged
    warnAfter: 800,           // If the check time of immutableCheck and serializableCheck exceeds 32ms, there will be a warning. Modify the warning time
    reducers: {},             // Custom add reducers
    middlewares: [],          // Custom add middlewares
    treatStore(store) {}      // Allows you to perform other processing on the store, such as mounting some monitoring methods
  };
}

Asynchronous injection of reducers

The *.async.{js,jsx,ts,tsx} files in the models folder will be considered as asynchronously injected reducers and will not be automatically loaded.
Configure asyncLoadReducers: true to enable asynchronous injection of reducers.

Api created by RTK RTKQuery cannot be loaded asynchronously because it needs to add middleware.

import { dynamicReducers } from 'umi-plugin-redux-toolkit/dynamicReducers';
import model_0 from './models/model_0';
import model_1 from './models/model_1';

function Component(props) {
 return <div />;
}

export default dynamicReducers([model_0, model_1])(Component); // Multiple models pass array
// or
export default dynamicReducers(model_0)(Component); // Single model

Get the store object

You can get the store object in the following way:

import { store } from '@@/plugin-reduxToolkit/store';