【发布时间】:2018-10-31 15:55:20
【问题描述】:
请您帮忙决定哪一种架构更好 (使用 react-native、react-redux、redux-thunk 从 API 获取列表)
- 示例
// component
componentDidMount() {
this.props.dispatch(fetchFunction());
}
// thunk action
fetchFunction () {
dispatch START_LOADING
return dispatch (
fetch()
dispatch SUCCESS_LOADING
);
}
- 示例
// component
componentDidMount() {
this.setState({'loading': true})
this.props.dispatch(fetchFunction()).finally(
this.setState({'loading': false})
);
}
//thunk action
fetchFunction () {
return dispatch (
fetch()
dispatch SUCCESS_LOADING
);
}
我的想法是在本地组件状态中存储“加载过程”?这种方法的优缺点是什么?
如我所见 - 示例 2:
如果加载需要更长的时间,我可以离开组件(它被卸载),我会看到警告 - “正在改变卸载组件的状态”
1 个例子: 保存了很多我在 redux 商店中不需要的额外数据(还有很多我需要从持久化中排除的数据),例如,如果我有一个网络商店产品组件,我将有很多 UI 道具存储在redux(例如包含 10 个产品的列表,每个产品都有自己的带有状态的按钮):( 但是我喜欢它,因为所有逻辑都保留在 redux-thunk-actions 中,并且我不需要关心组件在调度后如何继续编码,无论它是一个承诺还是不是示例:
this.props.dispatch(fetchFunction()).then(() => {});
this.props.dispatch(fetchFunction());
.. some code
到目前为止,我已经做了一些简单的项目,两种方式都很好。 能否请您给一些建议,以实现更大的项目?
【问题讨论】:
-
@Perniferous,如果没有一些解释,你的评论是没有用的,如果你有一些解释,它应该包含在答案中。
标签: react-native react-redux redux-thunk setstate