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

redux-brief

v0.3.23

Published

make redux easier to use

Downloads

43

Readme

redux-brief

make redux easier to use

这个库是对 Redux 的轻量级封装,完全兼容已有的 Redux 生态,相比直接使用 Redux ,有如下优点。

  • 完全消除 action 的模板代码
  • Typescript 类型安全
  • 内置 immer,提升开发体验
  • 使用 reducer 更加方便

Quickstart

yarn add redux-brief

API

步骤 1: 定义 Reducer 模块

user 模块

import { createModule } from 'redux-brief';
export const namespace = 'user';
export const state = {
  name: '',
  age: 0,
};

export const userModule = createModule({
  namespace,
  state,
  reducer: {
    setUserName(name: string, state) {
      state.name = name;
    },
    setAge(age: number, state) {
      state.age = age;
    },
  },
});

count 模块

import { createModule } from 'redux-brief';
export const namespace = 'count';
export const state = {
  money: 10,
  count: 10,
  count2: '',
};

export const countModule = createModule({
  namespace,
  state,
  reducer: {
    add(payload: number, state) {
      state.money += payload;
    },
    add2(payload: string, state) {
      state.count2 += payload;
    },
    minus(payload: number, state) {
      state.money -= 1;
    },
  },
});

步骤 2: 生成 Store

import { run } from 'redux-brief';
import thunk from 'redux-thunk';
import {
  countModule,
  namespace as countNamespace,
  state as countState,
} from './modules/count';
import {
  userModule,
  namesapce as userNamespace,
  state as userState,
} from './modules/user';

const modules = {
  [countNamespace]: countModule,
  [userNamespace]: userModule,
};

export const appState = {
  [countNamespace]: countState,
  [userNamespace]: userState,
};

export type AppState = typeof appState;
export type Modules = typeof modules;
const { store, reducers } = run<Modules>({ modules });
export { store, reducers };

步骤 3: 挂载 Store 到根组件上

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { Provider } from 'redux-brief';
import { store } from './store'; // 引入步骤2生成的 store

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

组件内使用

import React from 'react';
import { useSelector } from 'redux-brief';
import { AppState, reducers, store } from './store';

function App() {
  const money = useSelector((state: AppState) => state.count.money);
  const name = useSelector((state: AppState) => state.user.name);

  function minusAsync() {
    // 异步场景
    return (dispatch) => {
      setTimeout(() => {
        dispatch({
          type: 'count/minus',
        });
      }, 1000);
    };
  }

  const renderCount = () => {
    return (
      <div>
        <button
          onClick={() => {
            reducers.count.add(1);
          }}
        >
          {' '}
          加 1{' '}
        </button>

        <h1>money:{money}</h1>

        <button
          onClick={() => {
            store.dispatch(minusAsync() as any);
          }}
        >
          一秒后减 1
        </button>

        <button
          onClick={() => {
            reducers.user.setUserName('kobe bryant');
          }}
        >
          设置用户名
        </button>
        <h1>name:{name}</h1>
      </div>
    );
  };

  return <div className="App">{renderCount()}</div>;
}

export default App;

完整 demo 项目链接