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-plugin-build-common-component

v1.0.7

Published

用于封装大型dva公共业务组件

Downloads

11

Readme

dva插件:build-common-component

用于封装大型公共业务组件

对于公共的业务组件如果封装成state+生命周期的组件,当组件的体量较大时不便维护

如果还用普通的dva组件形式,共用同一份model数据,相互之间会产生干扰(组件可以实例化多份,但model是单例的)

本插件结合两者的优势,在model中加入prefix字段,数据的读写都自动代理到相应prefix中,使用起来更简单,一个model即可解决redux体系中 公共业务组件中model会产生相互干扰的情况

特点

  • 只有两个api($connect, $model),上手成本低
  • 组件更易于维护
  • 数据之间不会产生干扰

原理

  • 在原来model的基础上加一个字段prefix,注入model时,同一份数据根据prefix被分发为多份
  • 引用组件时,传入prefix,格式为{namespace: prefix} 这个参数决定了
  1. getUIState,找到相应的namespace时,会映射到哪个prefix上的数据
  2. getState 会在找到相应的model后,找到哪个prefix上的数据
  3. dispatch 时会将action.meta附加上这个参数,prefix
  4. reducer收到dispatch的action后,会根据action.meta的prefix,决定最终更新哪个prefix下的state

如何用

app.js 引入dva-plugin-build-common-component

项目引用 引入插件将实例化的dva,与connect传入得到$model, $connect 方法

import dva, {connect} from 'dva';
import buildCommonComponent from 'dva-plugin-build-common-component';

export const app = dva();

export const {$model, $connect} = buildCommonComponent(app, connect);

index.js 入口处注入公共组件的model

import app, {$model} from './app.js';
import modelList from './path/to/normal/models';
import commonModelList from './path/to/commonModels';


modelList.map(model => app.model(model));
commonModelList.map(model => $model(model)) // 在这里注入公共组件的model

编写公共组件 model 多加一个字段prefix,组件编写时使用$connect, 组件引用时传入相应的prefix

eg: model todo.js 相对之前多加一个prefix字段,需为数组格式

export default {
    namespace: 'count',
    prefix: ['count1', 'count2'], // 多加一个prefix字段, state 将会根据该字段被分发为多份
    state: {
        count: 0,
    },
    reducers: {
        addCount(state) {
            return {
                ...state,
                count: state.count++
            }
        }
    },
    //...
}

index.js 使用组件时,用$connect 包裹,第一个参数还是原来的mapStateToProps, 第二个参数为一个对象,对象内的方法会以参数方式传给UI

import {$connect} from 'app';
export default $connect(
    ({count})=> {
        return {
            count: count++
        }
    },
    {
        onAdd({dispatch, getState}, ...args) {
            dispatch({
                type: 'count/addCount'
            })
        }
    }
)(UI); 

function UI({count, onAdd}) {
  return ({onAdd}) => {
      return <div>
            <span onClick={onAdd()}>{count}</span>
        </div>
  }
}

UI引用时仅仅需要多传入一个参数prefix, prefix是一个对象

import Count from './path/../';

export default () => {
    return <div>
    // otherElement
    <Count {{prefix: {count: 'count1'}}}/>
    <Count {{prefix: {count: 'count2'}}}/>
</div>
}