All Projects → foca-js → foca

foca-js / foca

Licence: MIT license
流畅的React状态管理库

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to foca

react-native-boilerplate
React Native Boilerplate - React Native Starter Kits : react-navigation and its dependencies, redux, redux persist and redux thunk, redux toolkit, react native vector icons, react-native async storage
Stars: ✭ 68 (-27.66%)
Mutual labels:  react-redux, redux-toolkit
oc-redux
oc 版简易 redux 实现
Stars: ✭ 21 (-77.66%)
Mutual labels:  react-redux
cra-template-unicorn
🦄 The full template of create-react-app with typescript, redux-toolkit, react-redux, react-router for Single Page Application!
Stars: ✭ 56 (-40.43%)
Mutual labels:  redux-toolkit
ssr
React Server-Side Rendering Example
Stars: ✭ 265 (+181.91%)
Mutual labels:  react-redux
react-ssr
从零搭建一个react-ssr框架 解决页面js事件 样式同构 服务器客户端路由 数据注水脱水等问题
Stars: ✭ 42 (-55.32%)
Mutual labels:  react-redux
Color-Pic
Generate color palettes with image recognition
Stars: ✭ 21 (-77.66%)
Mutual labels:  react-redux
isomorphic-react-redux-saga-ssr
Isomorphic, React, Redux, Saga, Server Side rendering, Hot Module Reloading, Ducks, Code Splitting
Stars: ✭ 19 (-79.79%)
Mutual labels:  react-redux
react-native-boilerplate
Expo + Redux + React Navigation Pre-setup Template (expo SDK 44)
Stars: ✭ 142 (+51.06%)
Mutual labels:  react-redux
boss
React+express+sock.io+mongodb build a boss
Stars: ✭ 25 (-73.4%)
Mutual labels:  react-redux
admin-template-for-react
🌏 Admin template for React, React Redux, Redux Saga, React Router, i18n and integrated OAuth login
Stars: ✭ 83 (-11.7%)
Mutual labels:  react-redux
react-candee
A react framework that encapsulates the redux.
Stars: ✭ 30 (-68.09%)
Mutual labels:  react-redux
Auxilium
Emergency reporting app.
Stars: ✭ 29 (-69.15%)
Mutual labels:  react-redux
template-react-native-typescript
Minimal template with best practices and automation scripts for improved developer experience.
Stars: ✭ 19 (-79.79%)
Mutual labels:  redux-toolkit
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+6.38%)
Mutual labels:  react-redux
Covid19-Stats-IN
This app helps in tracking COVID-19 cases in India using covid19India apis
Stars: ✭ 13 (-86.17%)
Mutual labels:  react-redux
redux-analysis
《学习源码整体架构系列》多篇之redux源码,前端面试高频源码,微信搜索「若川视野」关注我,长期交流学习~
Stars: ✭ 28 (-70.21%)
Mutual labels:  react-redux
eshop
e-commerce website built with reactjs & redux on the frontend and lumen on the backend.
Stars: ✭ 27 (-71.28%)
Mutual labels:  react-redux
Turbo-Browser
Simple & Secure Internet mobile browser built on React Native.
Stars: ✭ 25 (-73.4%)
Mutual labels:  react-redux
redux-action
🍻 Flux action utilities for Redux
Stars: ✭ 25 (-73.4%)
Mutual labels:  react-redux
react-cli
基于 create-react-app 搭建的前端脚手架
Stars: ✭ 18 (-80.85%)
Mutual labels:  react-redux

FOCA

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

npm peer dependency version LGTM Grade GitHub Workflow Status (branch) Codecov npm npm bundle size (version) GitHub top language License Join the chat at https://gitter.im/foca-js/foca


mind map

特性

  • 模块化开发,拒绝模板
  • 专注 TS 极致体验,100%类型提示
  • 模型自动注册,导出即可使用
  • 内置 immer 快速处理数据
  • 支持 computed 计算属性,自动收集依赖
  • 自动管理异步函数的 loading 状态
  • 支持私有方法,仅在内部调用
  • 可定制的多引擎数据持久化
  • 允许同类 redux 库并存,迁移无忧

安装

yarn add foca

创建模型

actions 修改数据

import { defineModel } from 'foca';

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

export const counterModel = defineModel('counter', {
  initialState,
  actions: {
    // 支持无限参数
    plus(state, value: number, times: number = 1) {
      state.count += value * times;
    },
    minus(state, value: number) {
      return { count: state.count - value };
    },
  },
});

computed 计算属性

export const counterModel = defineModel('counter', {
  initialState,
  // 自动收集依赖
  computed: {
    filled() {
      return Array(this.state.count)
        .fill('')
        .map((_, index) => index)
        .map((item) => item * 2);
    },
  },
});

effects 副作用

export const counterModel = defineModel('counter', {
  initialState,
  actions: {
    increment(state) {
      state.count += 1;
    },
  },
  effects: {
    async incrementAsync() {
      await this._sleep(100);

      this.increment();
      // 也可直接修改状态而不通过actions,仅在内部使用
      this.setState({ count: 1 });
      this.setState((state) => {
        state.count += 1;
      });

      return 'OK';
    },
    // 私有方法,仅在内部调用
    _sleep(duration: number) {
      return new Promise((resolve) => {
        setTimeout(resolve, duration);
      });
    },
  },
});

events 事件回调

export const counterModel = defineModel('counter', {
  initialState,
  events: {
    // 模型初始化
    onInit() {
      console.log(this.state);
    },
    // 模型数据变更
    onChange(prevState, nextState) {},
  },
});

clone 复制模型

import { cloneModel } from 'foca';
import { counterModel } from './counterModel';

export const counter2Model = cloneModel('counter2', counterModel);

export const counter3Model = cloneModel('counter3', counterModel, {
  initialState: { count: 100 },
});

export const counter4Model = cloneModel('counter4', counterModel, (prev) => {
  return {
    initialState: { count: prev.count * 2 },
  };
});

使用

在 function 组件中使用

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

const App: FC = () => {
  const count = useModel(counterModel, (state) => state.count);
  const loading = useLoading(counterModel.incrementAsync);

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

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

export default App;

在 class 组件中使用

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.incrementAsync();
  }

  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.incrementAsync),
  };
};

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
Nextjs 案例仓库:https://github.com/foca-js/foca-demo-nextjs

生态

请求

仓库 版本 描述 平台
axios npm 当下最流行的请求库 React, RN
foca-axios npm 针对 axios 的增强型适配器 React, RN
foca-miniprogram-axios npm 针对 axios 的增强型适配器 Taro, Remax

存储引擎

仓库 版本 描述 平台
react-native-async-storage npm React-Native 持久化引擎 RN
foca-taro-storage npm Taro 持久化引擎 Taro
localForage npm 浏览器端持久化引擎 React

日志

仓库 版本 描述 平台
@redux-devtools/extension npm 浏览器日志插件 React, RN
react-native-debugger npm 日志应用程序 RN
redux-logger npm 控制台输出日志 React, RN
Taro, Remax

捐赠

开源不易,升级维护框架和解决各种 issue 需要十分多的精力和时间。希望能得到你的支持,让项目处于良性发展的状态。捐赠地址:二维码

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].