简体   繁体   English

反应redux bindActionCreators的用法

[英]React redux bindActionCreators usage

i have a file inside action directory which is root.js. 我在操作目录下有一个文件,它是root.js。 Root.js will compile all the others action inside, and i bind it with bindActionCreators Root.js将在内部编译所有其他动作,我将其与bindActionCreators绑定

export const all = (store) => {
    AUTH: bindActionCreators(AUTH.actions, store.dispatch),
    ....: .....
}

From what i learned, bindActionCreators is for the purpose of auto dispatching the action. 据我了解,bindActionCreators是用于自动分派动作的目的。 If that is the case, then how do i access it from smart component? 如果是这种情况,那么我该如何从智能组件访问它?

I see things like dispatch(action). 我看到了诸如dispatch(action)之类的东西。 But since now i already bind it globally, i dont think that i would need to specify dispatch anymore. 但是由于现在我已经在全球范围内绑定了它,所以我认为我不再需要指定调度了。 How do i do it, or is there any part that i misunderstood? 我该怎么做,还是我误解了任何部分? Thank you 谢谢

bindActionCreators - will create an object of actions each wrapped with the dispatch. bindActionCreators将创建一个动作对象,每个动作对象都包装在分派中。
It's good for passing them as refs to non-connected components that should not know anything about redux or dispatch . 最好将它们作为ref传递给不知道有关reduxdispatch未连接组件。
Quote from the DOCS : 引用DOCS

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. bindActionCreators的唯一用例是,当您要将一些动作创建者传递给一个不了解Redux的组件,并且您不想将调度或Redux存储传递给该组件时。

So if you want that connected component to pass action creators to a dumb component, you can set an object via bindActionCreators and pass it with props to the dumb component. 因此,如果您希望连接的组件将动作创建者传递给哑组件,则可以通过bindActionCreators设置对象,并将其与道具一起传递给哑组件。
Example: 例:

const myActionCreators = bindActionCreators(Auth.myActions, dispatch)
<DumbComponent {...myActionCreators} />

The recommended approach is to have each connected component file import the action creators it needs, and use the "object shorthand" supported by connect : 推荐的方法是让每个连接的组件文件导入所需的操作创建者,并使用connect支持的“对象简写”:

import {addTodo, toggleTodo} from "./todoActions";

const actions = {addTodo, toggleTodo};

export default connect(null, actions)(TodoList);
// each TodoList instance now has this.props.addTodo and
// this.props.toggleTodo, which will dispatch actions when called.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM