ice-lab / icestore

🌲 Simple and friendly state for React
MIT License
397 stars 35 forks source link

feat: withModel #104

Closed alvinhui closed 4 years ago

alvinhui commented 4 years ago

需求背景

https://github.com/ice-lab/icejs/issues/244

API

模型定义

export default {
  state: {
    count: 0
  },
  reducers: {
    increment (prevState) => ({ ...prevState, count: prevState.count + 1 }),
  },
};

模型消费

import { withModel } from '@ice/store';
import model from './model';

function Component({ model }) {
  const [ state, dispatchers ] = model.useValue();
}

export default withModel(model)(Component);

参考 ModelAPIs 的定义 获取所有对外的 API。

说明

该 PR 中包含示例及文档的更新。

codecov-io commented 4 years ago

Codecov Report

Merging #104 into release/1.4.0 will decrease coverage by 3.95%. The diff coverage is 39.13%.

Impacted file tree graph

@@                Coverage Diff                @@
##           release/1.4.0     #104      +/-   ##
=================================================
- Coverage          87.91%   83.95%   -3.96%     
=================================================
  Files                 20       19       -1     
  Lines                546      561      +15     
  Branches             121      121              
=================================================
- Hits                 480      471       -9     
- Misses                56       80      +24     
  Partials              10       10              
Impacted Files Coverage Δ
src/plugins/error.tsx 88.88% <ø> (+6.94%) :arrow_up:
src/plugins/loading.tsx 88.33% <ø> (+7.17%) :arrow_up:
src/index.tsx 83.33% <10.00%> (ø)
src/plugins/modelApis.tsx 60.00% <44.06%> (-23.73%) :arrow_down:
alvinhui commented 4 years ago

针对目前 API 太多且有些不一致的问题,其实目前内部的实现可以允许这样使用 createStore 方法:

import { createStore, withModel } from '@ice/store';

// 使用 createStore
const { Provider, getModelAPIs } = createStore({ model });
const { useValue, withValue, useState, getValue, getState } = getModelAPIs('model');

function Component() {
  const [ state ] = useValue();
}

// 使用 withModel
function Component({ model }) {
   const [ state ] = model.useValue();
}
withModel(model)(Component);
chenbin92 commented 4 years ago

API 收敛的问题要在这个 MR 处理吗,还是另外在发版本支持?

alvinhui commented 4 years ago

API 收敛的问题要在这个 MR 处理吗,还是另外在发版本支持?

建议不在这个 MR 上处理,未来再发版本支持。两个步骤:

chenbin92 commented 4 years ago

与 @alvinhui 讨论目前遗留两个问题另外新建 issue 处理:

  1. 导出 API 太多需要优化
  2. 两个 withModel