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

mozz

v0.1.5

Published

> 本来想叫moz的,结果发布npm包的时候,发现mo a ~ z 都被占了 ~

Downloads

13

Readme

mozz

本来想叫moz的,结果发布npm包的时候,发现mo a ~ z 都被占了 ~

基于react hooks实现的状态管理工具。

GitHub npm version npm downloads React

特性

  • 基于React hooks 实现,支持所有官方与自定义hooks
  • 超级轻量,核心代码只有十来行
  • 无侵入性,可单独使用,也可以与redux,mobx配合使用
  • 支持Typescript,可轻松获取返回的状态类型,无需额外定义声明
  • 使用简单,基本与hooks方式一致

安装

> yarn add mozz

使用

首先自定义一个hook,这个hooks没有任何特殊,与其他自定义hooks完全一样。

// useCounter.ts
import {useState} from "react";

export default function useCounter() {
  const [counter, setCounter] = useState(0);

  return {
    counter,
    decrease: () => setCounter(counter - 1),
    increase: () => setCounter(counter + 1)
  }
}

然后在父级组件中,引入mozz,以及刚才自定义好的hook。 利用api createStore得到特定的 Provider与hook函数useStore

import { createStore } from 'mozz';
import useCounter from './useCounter';

export const { Provider, useStore } = createStore(useCounter);

父级组件作为目标组件,需要被Provider包裹起来

export default () => <Provider><App /></Provider>;

在父级组件内部,可直接使用 useStore 获取自定义hook中,定义好的状态与方法。

const App: FC = () => {
  const { counter, decrease, increase } = useStore();
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <a className="App-link" href="/" rel="noopener noreferrer">
          Learn moz
        </a>

        <div>
          <p>{counter}</p>
          <button onClick={increase}>+</button>
          <button onClick={decrease}>-</button>
        </div>
        <Comp />
      </header>
    </div>
  );
};

子组件 Comp 中,也可以直接使用 useStore 获取状态与方法,他们被父子组件共享。

import React from 'react';
import {useStore} from './App';

export default function Comp() {
  const {increase} = useStore();

  return (
    <div>
      <p style={{ fontSize: '12px' }}>我是子组件,当我内部的点击事件执行时,能够影响到counter的值</p>
      <button onClick={increase}>sub +</button>
    </div>
  )
}

OK,没有啦,使用就跟useState一样简单。