在 Redux 中,store.dispatch()、connect() 和 bindActionCreators() 之间的关系是什么
In Redux, what is the relationship between store.dispatch(), connect() and bindActionCreators()
dispatch()
、connect()
和bindActionCreators()
有什么区别?
我应该在什么情况下使用它们?
谢谢
dispatch - 调度一个操作来触发 redux 的存储 中的更改。执行此更改的逻辑在减速器中。
connect - 特定组件的 React 状态与 redux 的存储无关,直到您将高阶组件 connect() 应用于该特定组件。 为了让redux store 和react 的state 一起工作,你需要connect。组件连接到 redux 的 store 后,就可以监听 store 的变化。如果一个动作已经被分派,redux store 发生变化,并且因为你的组件连接到 store 并监听这些变化,它需要重新渲染。但是,有一个小问题 - 您还需要指定您的组件将重新呈现哪些存储更改 (mapStateToProps) 以及您的组件允许触发哪些状态更改 (mapDispatchToProps)
bindActionCreators - 包装动作创建者(创建动作的函数)进入 dispatch() 调用 这样你就可以像这样使用它:fancyActionCreator() 而不是必须将它包装在 dispatch(fancyActionCreator()) 中。
The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch or the Redux store to it.
dispatch()
、connect()
和bindActionCreators()
有什么区别?
我应该在什么情况下使用它们?
谢谢
dispatch - 调度一个操作来触发 redux 的存储 中的更改。执行此更改的逻辑在减速器中。
connect - 特定组件的 React 状态与 redux 的存储无关,直到您将高阶组件 connect() 应用于该特定组件。 为了让redux store 和react 的state 一起工作,你需要connect。组件连接到 redux 的 store 后,就可以监听 store 的变化。如果一个动作已经被分派,redux store 发生变化,并且因为你的组件连接到 store 并监听这些变化,它需要重新渲染。但是,有一个小问题 - 您还需要指定您的组件将重新呈现哪些存储更改 (mapStateToProps) 以及您的组件允许触发哪些状态更改 (mapDispatchToProps)
bindActionCreators - 包装动作创建者(创建动作的函数)进入 dispatch() 调用 这样你就可以像这样使用它:fancyActionCreator() 而不是必须将它包装在 dispatch(fancyActionCreator()) 中。
The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch or the Redux store to it.