GithubHelp home page GithubHelp logo

amcloc / redux-model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from redux-model/redux-model

0.0 0.0 0.0 1.84 MB

流畅的Redux模型,TypeScript项目利器,支持 React, RN, Taro 和 Vue

Home Page: https://redux-model.github.io/redux-model

License: MIT License

Shell 0.62% TypeScript 99.21% JavaScript 0.17%

redux-model's Introduction

English Document

Redux-Model是为了弥补原生Redux繁琐的开发流程,开发者重复劳动效率低下,模板文件导致代码量臃肿,以及因action和reducer文件分散造成代码追踪困难的问题而设计的。

License GitHub Workflow Status (branch) Codecov

特性

  • 深度封装,模块化开发
  • 使用mvvm快速处理reducer
  • 👍真正意义上的Typescript框架,写起来比JS更流畅
  • 内置http服务,请求action自带loading追踪、数据节流
  • 支持React/Vue Hooks
  • 支持数据持久化

安装

React 或 React-Native

npm install @redux-model/react redux react-redux

Vue v3

npm install @redux-model/vue redux

Taro v3

npm install @redux-model/taro redux react-redux

其它

定义模型

interface Response {
  id: number;
  name: string;
}

interface Data {
  counter: number;
  users: Partial<{
    [key: string]: Response;
  }>;
}

class TestModel extends Model<Data> {
  plus = this.action((state, step: number = 1) => {
    state.counter += step;
  });

  getUser = $api.action((id: number) => {
    return this
      .get<Response>(`/api/users/${id}`)
      .onSuccess((state, action) => {
        state.users[id] = action.response;
      });
  });

  protected initialState(): Data {
    return {
      counter: 0,
      users: {},
    };
  }
}

export const testModel = new TestModel();

执行Action

testModel.plus();
testModel.plus(2);

testModel.getUser(3);
testModel.getUser(5).then(({ response }) => {});

在 Hooks 中获取数据

const data = testModel.useData(); // { counter: number, users: object }

const counter = testModel.useData((data) => data.counter); // number
const users = testModel.useData((data) => data.users); // object

const loading = testModel.getUser.useLoading(); // boolean

在 connect 中获取数据

type ReactProps = ReturnType<typeof mapStateToProps>;

const mapStateToProps = => {
  return {
    counter: testModel.data.counter, // number
    users: testModel.data.users, // object
    loading: testModel.getUser.loading, // boolean
  };
};

export default connect(mapStateToProps)(App);

在线运行示例

文档

请点击这里查看文档


欢迎使用并随时给我建议

redux-model's People

Contributors

fwh1990 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.