# foca
**Repository Path**: mirrors/foca
## Basic Information
- **Project Name**: foca
- **Description**: 流畅的 React 状态管理库,基于 redux 和 react-redux
- **Primary Language**: TypeScript
- **License**: MIT
- **Default Branch**: master
- **Homepage**: https://www.oschina.net/p/foca
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 0
- **Created**: 2022-04-19
- **Last Updated**: 2025-07-26
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# FOCA
流畅的 react 状态管理库,基于[redux](https://github.com/reduxjs/redux)和[react-redux](https://github.com/reduxjs/react-redux)。简洁、极致、高效。
[](https://github.com/facebook/react)
[](https://github.com/microsoft/TypeScript)
[](https://github.com/foca-js/foca/actions)
[](https://codecov.io/gh/foca-js/foca)
[](https://www.npmjs.com/package/foca)
[](https://www.npmjs.com/package/foca)
[](https://bundlephobia.com/package/foca@latest)
[](https://github.com/foca-js/foca/blob/master/LICENSE)
[](https://github.com/prettier/prettier)

# 特性
- 模块化开发,导出即可使用
- 专注 TS 极致体验,超强的类型自动推导
- 内置 [immer](https://github.com/immerjs/immer) 响应式修改数据
- 支持计算属性,自动收集依赖,可传参数
- 自动管理异步函数的 loading 状态
- 可定制的多引擎数据持久化
- 支持局部模型,用完即扔
- 支持私有方法
# 使用环境
- Browser
- React Native
- Taro
- Electron
# 安装
```bash
# npm
npm install foca
# yarn
yarn add foca
# pnpm
pnpm add foca
```
# 初始化
```typescript
import { store } from 'foca';
store.init();
```
# 创建模型
### reducers 修改数据
```typescript
import { defineModel } from 'foca';
const initialState: { count: number } = { count: 0 };
export const counterModel = defineModel('counter', {
initialState,
reducers: {
// 支持无限参数
plus(state, value: number, times: number = 1) {
state.count += value * times;
},
minus(state, value: number) {
return { count: state.count - value };
},
},
});
```
### computed 计算属性
```typescript
export const counterModel = defineModel('counter', {
initialState,
// 自动收集依赖
computed: {
filled() {
return Array(this.state.count)
.fill('')
.map((_, index) => index)
.map((item) => item * 2);
},
},
});
```
### methods 组合逻辑
```typescript
export const counterModel = defineModel('counter', {
initialState,
reducers: {
increment(state) {
state.count += 1;
},
},
methods: {
async incrementAsync() {
await this._sleep(100);
this.increment();
// 也可直接修改状态而不通过reducers,仅在内部使用
this.setState({ count: this.state.count + 1 });
this.setState((state) => {
state.count += 1;
});
return 'OK';
},
// 私有方法,外部使用时不会提示该方法
_sleep(duration: number) {
return new Promise((resolve) => {
setTimeout(resolve, duration);
});
},
},
});
```
### events 事件回调
```typescript
export const counterModel = defineModel('counter', {
initialState,
events: {
// 模型初始化
onInit() {
console.log(this.state);
},
// 模型数据变更
onChange(prevState, nextState) {},
},
});
```
# 使用
### 在 function 组件中使用
```tsx
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 (
arcsin1 |
xiongxliu |