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

prematch

v1.1.0

Published

Erc 前端开发团队研发的一款轻量级状态管理工具

Downloads

10

Readme

Prematch

Prematch 是 ERC 前端开发团队研发的一款轻量级状态管理工具,使用和 Rematch 一样的API。Gzip 后大小近2KB,与市面的状态管理工具相比,更低碳更适合移动端使用。

特性

Rematch 是 Redux 的最佳实践。它不需要多余的 action types,action creators 和 switch。Prematch 实现了它一样的API的同时,还具备以下优秀特性:

  • 低碳轻量,不需要引入 Redux,Gzip之后的大小不到1kB
  • 易学易用,Prematch 实现了跟 Rematch 一样的Api和模式,对 Redux 和 Rematch 用户尤其友好
  • elm 概念,通过 reducers, effects 和 state 组织 model
  • 插件机制,支持非 Redux 的官方 plugin,比如 rematch-loading。 可以自动处理 loading 状态,不用一遍遍地写 showLoading 和 hideLoading

Getting Started

npm install @prematch/core

Step 1: Init

init 用来配置你的 reducers, devtools & store。

index.js

import { init } from '@prematch/core'
import * as models from './models'

const store = init({
  models,
})

export default store

Step 2: Models

model 将 state, reducers, 异步 actions 放在同一个地方。

models.js

export const count = {
  state: 0, // initial state
  reducers: {
    // handle state changes with pure functions
    increment(state, payload) {
      return state + payload
    }
  },
  effects: (dispatch) => ({
    // handle state changes with impure functions.
    // use async/await for async actions
    async incrementAsync(payload, rootState) {
      await new Promise(resolve => setTimeout(resolve, 1000))
      dispatch.count.increment(payload)
    }
  })
}

回答几个问题有助于理解 modal:

  1. 初始的 state 是什么? state
  2. 如何改变 state? reducers
  3. 如何处理异步 actions? effects 使用 async/await

Step 3: Dispatch

dispatch 是触发 model 中 reducers 和 effects 的方式。Dispatch 标准化了 action,而无需编写 action types 或者 action creators。

import { init } from '@rematch/core'
import * as models from './models'

const store = init({
  models,
})

export const { dispatch } = store
                                                  // state = { count: 0 }
// reducers
dispatch({ type: 'count/increment', payload: 1 }) // state = { count: 1 }
dispatch.count.increment(1)                       // state = { count: 2 }

// effects
dispatch({ type: 'count/incrementAsync', payload: 1 }) // state = { count: 3 } after delay
dispatch.count.incrementAsync(1)                       // state = { count: 4 } after delay

Dispatch 能被直接调用,或者用 dispatch[model][action](payload) 简写。

Step 4: View

Prematch 提供的 React 高阶组件可以直接连接,而不需要引入 Redux。可以看下面的例子:

import React, { Component } from 'react';
import { Button } from 'antd-mobile';
import indexModel from './model';
import store, { getStoreConnect } from '@src/store';
import './index.scss';


store.model(indexModel);

@getStoreConnect(['index', 'num'])
class Index extends Component {

  add = () => {
    const { dispatch } = this.props;
    dispatch.index.add(10)
  }

 render() {
    const { num } = this.props;

    return (
      <div>
        index {num}
        <Button onClick={this.add} >reducers测试</Button>
      </div>
    );
  }
}

export default Index;

Hooks

提供和react-redux类似的useSelector和useDispatch 可在Functional Component中通过hooks连接store 假设已经注册了Step2的model: count

import React from 'react';
import { useSelector, useDispatch } from 'prematch';

export default () => {
    const dispatch = useDispatch();
    // const dispatch = useDispatch(d => d.count)
    const { num } = useSelector(s => s.count);
    // const { num } = useSelector('count', 'num');
    
    return (
        <div>
            num: {num}
            <button onClick={() => dispatch.count.incrementAsync(5)}>
                click to add
            </button>
        </div>
    )
}