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

dva-reset-state

v0.1.3

Published

dva reset state plugin

Downloads

40

Readme

dva-reset-state

dva reset state plugin , 页面离开清理 dva 或 redux 中相关数据

NPM JavaScript Style Guide

Install

npm install --save dva-reset-state

Usage

  • 注册 src/index.tsx
import React, { Component } from 'react'

import createResetState from 'dva-reset-state'

const app: any = dva()
app.use(createResetState())
  • 在 umi@3 中使用

app.ts

import { plugin } from 'umi';

import createResetState from 'dva-reset-state';

// 注册插件
plugin.register({
  apply: {
    dva: {
      plugins: [createResetState(true)],// 对于引用类型的字段,重置后每次都返回新的引用,从而触发useEffect的更新
    },
  },
  path: 'any', // 随意填写个字符串,否则报错
});
  • 在需要重置状态的组件中使用
  1. class 组件
import { resetStateWillUnmount } from "dva-reset-state";

@connect() // connect必须写在resetState上面
@resetStateWillUnmount("product")
export default class SQLManage extends Component<any> {
  render(){
    return <div>uuu</div>
  }
}
  1. 函数组件
import React from "react";
import { useResetStateWillUnmount } from "dva-reset-state";

const EditProduct = () => {
  useResetStateWillUnmount({ editProduct: "productInfo" });
  return <div>ooo</div>;
};

export default EditProduct;
  1. 在任何事件或函数中使用
  • 函数组件
import React from "react";
import { useResetState } from "dva-reset-state";

const Demo = () => {
  const resetState = useResetState()

  const onClick = () => {
    resetState(["userList", { productList: ["list", "pageInfo"] }]);
  };
  return (
    <div>
      <button onClick={onClick}></button>
    </div>
  );
};

export default Demo;
  • class 组件
import React from "react";
import { connect } from "dva";
import { reset } from "dva-reset-state";

@connect()
export default class Demo extends React.Component<any>{
     onClick = () => {
        reset(this.props.dispatch,["userList", { productList: ["list", "pageInfo"] }]);
      };
    render(){
        return (
            <div>
              <button onClick={this.onClick}></button>
            </div>
          );
    }
}

重置哪些数据?

  1. 重置所有 state,不传递参数
@resetStateWillUnmount()

useResetStateWillUnmount();
  1. 重置单个 namespace 数据
@resetStateWillUnmount("editProduct")

useResetStateWillUnmount("editProduct");
  1. 重置多个 namespace 下的所有数据
@resetStateWillUnmount(["productList","editProduct"])

useResetStateWillUnmount(["productList","editProduct"]);
  1. 重置单个 namespace 下的单个数据
@resetStateWillUnmount({productList:"list"})

useResetStateWillUnmount({productList:"list"});
  1. 重置单个 namespace 下的多个数据
@resetStateWillUnmount({productList:["list","total"]})

useResetStateWillUnmount({productList:["list","total"]});
  1. 重置多个 namespace 下的多个数据
@resetStateWillUnmount({productList:["list","total"],editProduct:"productInfo"})

useResetStateWillUnmount({productList:["list","total"],editProduct:"productInfo"});
  1. 重置某些 namespace 下所有字段或者重置某些 namespace 下部分字段
@resetStateWillUnmount(['userList', { productList: ['list', 'pageInfo'] }])

useResetStateWillUnmount(['userList', { productList: ['list', 'pageInfo'] }]);
  1. 重置给定 namespace 以外的 state

    第三个参数需要设置为 true

@resetStateWillUnmount(['userList', { productList: ['list', 'pageInfo'] }], true)

useResetStateWillUnmount(['userList', { productList: ['list', 'pageInfo'] }], true);

reset(store.dispatch,'userList',true)
reset(store.dispatch,['userList','editProduct'],true)

reset(
  store.dispatch,
  ['userList', { editProduct: ['productInfo'] }]
  true
)

License

MIT © balibabu