GithubHelp home page GithubHelp logo

nicbair / foca Goto Github PK

View Code? Open in Web Editor NEW

This project forked from foca-js/foca

0.0 0.0 0.0 2.1 MB

流畅的react状态管理库

Home Page: https://foca.js.org

License: MIT License

JavaScript 0.28% TypeScript 99.64% Shell 0.08%

foca's Introduction

FOCA

流畅的 react 状态管理库,基于reduxreact-redux。简洁、极致、高效。

License GitHub Workflow Status (branch) Codecov npm npm bundle size (version) GitHub top language npm peer dependency version


mind map

特性

  • 模块化开发
  • 专注 typescript 极致体验
  • 模型自动注册,导出即可使用
  • 内置 immer 快速处理数据
  • 智能追踪异步函数的执行状态
  • 模型支持私有方法
  • 可定制的多引擎数据持久化
  • 数据隔离,允许同类状态库并存

安装

yarn add foca

使用

定义模型

// File: counterModel.ts
import { defineModel } from 'foca';

const initialState: { count: number } = {
  count: 0,
};

// 无须手动注册到store,直接导出到react组件中使用
export const counterModel = defineModel('counter', {
  // 初始值,必填属性,其他属性均可选
  initialState,
  actions: {
    // state可自动提示类型 { count: number }
    plus(state, value: number, double: boolean = false) {
      // 直接修改状态
      state.count += value * (double ? 2 : 1);
    },
    minus(state, value: number) {
      // 直接返回新状态
      return { count: state.count - value };
    },
    // 私有方法,只能在模型内部被effect方法调用,外部调用则TS报错(属性不存在)
    _clear(state) {
      return this.initialState;
    },
  },
  effects: {
    // 异步函数,自动追踪执行状态(loading)
    async doSomething() {
      // 调用私有方法
      await this._sleep(100);

      // 快速处理状态,对于网络请求的数据十分方便
      this.setState({ count: 1 });
      this.setState((state) => {
        state.count += 1;
      });
      // 调用action函数处理状态
      this.plus(1, true);

      // 调用effect函数
      return this.commonUtil(1);
    },
    // 普通函数
    commonUtil(x: number) {
      return x + 1;
    },
    // 私有方法,只能在模型内部使用,外部调用则TS报错(属性不存在)
    _sleep(duration: number) {
      return new Promise((resolve) => {
        setTimeout(resolve, duration);
      });
    },
  },
  hooks: {
    // store初始化完成后触发onInit钩子
    onInit() {
      this.plus(1);
      console.log(this.state);
    },
  },
});

在函数组件中使用

import { FC, useEffect } from 'react';
import { useModel, useLoading } from 'foca';
import { counterModel } from './counterModel';

const App: FC = () => {
  // count类型自动提示 number
  const { count } = useModel(counterModel);
  // 仅effects的异步函数能作为参数传入,其他函数TS自动报错
  const loading = useLoading(counterModel.doSomething);

  useEffect(() => {
    counterModel.doSomething();
  }, []);

  return (
    <div onClick={() => counterModel.plus(1)}>
      {count} {loading ? 'Loading...' : null}
    </div>
  );
};

export default App;

在类组件中使用

import { Component } from 'react';
import { connect, getLoading } from 'foca';
import { counterModel } from './counterModel';

type Props = ReturnType<typeof mapStateToProps>;

class App extends Component<Props> {
  componentDidMount() {
    counterModel.doSomething();
  }

  render() {
    const { count, loading } = this.props;

    return (
      <div onClick={() => counterModel.plus(1)}>
        {count} {loading ? 'Loading...' : null}
      </div>
    );
  }
}

const mapStateToProps = () => {
  return {
    count: counterModel.state.count,
    loading: getLoading(counterModel.doSomething),
  };
};

export default connect(mapStateToProps)(App);

文档

https://foca.js.org

例子

沙盒在线试玩:https://codesandbox.io/s/foca-demos-e8rh3
React 案例仓库:https://github.com/foca-js/foca-demo-web
RN 案例仓库:https://github.com/foca-js/foca-demo-react-native
Taro 案例仓库:https://github.com/foca-js/foca-demo-taro

生态

仓库 版本 描述 平台
axios npm 当下最流行的请求库 React, RN
foca-axios npm 针对 axios 的增强型适配器 React, RN
foca-miniprogram-axios npm 针对 axios 的增强型适配器 Taro, Remax
foca-taro-storage npm Taro 持久化引擎 Taro
rn-async-storage npm React-Native 持久化引擎 RN
localForage npm 浏览器端持久化引擎 React
redux-logger npm 控制台打印 redux 日志 React, RN
Taro, Remax

foca'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.