React在组件中直接使用dispatch好呢 还是用action函数来处理优雅?

componentWillUnmount() {
        this.timer && clearTimeout(this.timer);
        this.props.dispatch({type : 'USER_ALL_CLEAR'});
    }
componentWillUnmount() {
        this.timer && clearTimeout(this.timer);
        this.props.userAction.clearAll();
    }

那种优雅????

阅读 8.8k
4 个回答

第二种。type 最好定义在一个地方,不然容易敲错。

第一种。修改状态更提倡dispatch一个action

第一种 请看redux

新手上路,请多包涵

单个组件用第二种,多组件共享一个state用第一种。都很优雅。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题