-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release: 1.0.0 Beta #59
Conversation
…0-beta # Conflicts: # package.json
不用区分就是下面这样是吧? 在 redux 之类的概念了,reducers 的概念类似 // src/models/count.ts
export default {
state: {
count: 0
},
addBy: (prevState, payload) => {...prevState, count: prevState.count + payload},
addByAsync: async (payload, state, actions) => {
await delay(1000)
// 调用本模型或其他模型的 action
actions.count.addBy(payload)
}
} |
不知道你说的很不友好是指的什么,这里我觉得有一点不友好的是 // src/models/count.ts
export default {
// 你说的 namespace 是指这样吧?
namespace: 'count'
effects: {
async addByAsync(payload, state, actions) {
await delay(1000)
// 调用本模型或其他模型的 action
actions.count.addBy(payload)
}
}
} |
这个需要看下代码实现,暂时可以作为后续的优化点迭代? |
是的,就是「在定义 store 的时候需要知道 modelName,但是 store 里并没有 modelName 的概念」,这点不友好。 是的,namespace 就是你示例中这样。我个人比较倾向于 async addByAsync(payload, state, getActions) {
getActions(); // 获取本模型的 actions
geActions('user') // 获取其他模型的 actions
} 这样处理。 |
+1 effectState 和 modelState 分开我觉得有一些好处。 比方说在未 useEffectState 的组件里,是不会订阅异步 action 的 loading 变化的。 |
那完整的定义 store 应该就是下面这样? // src/models/count.ts
export default {
namespace: 'count'
reducers: {
addBy(prevState, payload) => {...prevState, count: prevState.count + payload}
},
effects: {
async addByAsync(payload, state, getActions) {
// 每次定义 store 都要调用这一步,我感觉有点多余
// 我偏向这种 actions.count.xxx
const actions = getActions() // or getActions('user')
await delay(1000)
// 这里有点没看懂
// 1. 这里的 state 表示什么?没看到被使用?
// 2. 这里的 payload 我理解 view 调用时的参数,然后直接传给 payload 了? 正常应该是这里异步返回的值是吧?
actions.count.addBy(payload)
}
} |
(state) => ({ ...state, subTitle: 'SubTitle' }), | ||
(actions) => actions, | ||
(effectsState) => effectsState, | ||
)(TodoList); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果这里要用 user Model 要怎么写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
components/Todos.tsx 下有 useModel 的写法,如果是 Class 则必须是用 connect。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
提供 withModel,withModels 两个 API ,后者可以获取多个 models
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
const { connect } = store; | ||
|
||
class TodoList extends Component<any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 示例我觉得保持一致写法就可以了。都用 Function Component
- 如果要演示 connect 单独写个非常简单的 example 或者文档透出就行了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
单独也一个 example 感觉成本太高了。希望是只有一个 example 然后覆盖所有用法?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
默认用 FC component,然后有一个 Class component 的示例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/createStore.tsx
Outdated
} | ||
|
||
function useModelEffectState(namespace: string) { | ||
const [, , , useModelEffectState ] = modelContainers[namespace]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
话说这种写法不如对象 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我已经爱上这种写法了😆
纯对象的话,好处就是:
redux style 的好处就是:
结论: export default {
state: {},
actions: {
add(){},
async addAsync() {}
}
} |
// todos model
// 1
const actions = getActions()
actions.add();
const userActions = getActions('user');
userActions.foo();
// 2
actions.todos.add(); // namespace
actions.user.foo();
// 3
// payload, state, actions, globalActions
actions.add,
globalActions.user.foo
globalActions.product.foo
// 4
this.add
actions.user.foo 选择第三种。 |
done. |
src/createStore.tsx
Outdated
]; | ||
} | ||
|
||
function useModels(namespaces: string[], createMapState?, createMapActions?, createMapActionsState?) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不符合 Hooks 的规则
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@alvinhui +1 |
使用示例
https://github.com/ice-lab/icestore/tree/release/1.0.0-beta/examples/todos
能力表
API
模型定义
模型注册
绑定视图
模型消费
问题和讨论点
模型的定义是否需要区分 effects 和 reducers;
当前在 effect 调用本模型的 reducers 不是很友好,有几种解决方式:
this.actionName由于 effectState 和 modelState 是分开的,所以会多一次将 isLoaing 设置为 false 的 setState 的触发。