Skip to main content

bindActionCreators(actionCreators, dispatch)

概述

¥Overview

将值为 动作创造者 的对象转换为具有相同键的对象,但每个操作创建者都封装到 dispatch 调用中,以便可以直接调用它们。

¥Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch call so they may be invoked directly.

通常,你应该直接在 Store 实例上调用 dispatch。如果你将 Redux 与 React 结合使用,react-redux 将为你提供 dispatch 函数,因此你也可以直接调用它。

¥Normally you should just call dispatch directly on your Store instance. If you use Redux with React, react-redux will provide you with the dispatch function so you can call it directly, too.

bindActionCreators 的唯一用例是当你想要将一些操作创建者传递给不支持 Redux 的组件,并且你不想将 dispatch 或 Redux 存储传递给它时。

¥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.

为了方便起见,你还可以传递一个动作创建者作为第一个参数,并获得一个调度封装函数作为返回。

¥For convenience, you can also pass an action creator as the first argument, and get a dispatch wrapped function in return.

警告

这最初是为了与旧版 React-Redux connect 方法一起使用。它仍然有效,但很少需要。

¥This was originally intended for use with the legacy React-Redux connect method. It still works, but is rarely needed.

参数

¥Parameters

  1. actionCreators(函数或对象):动作创造者,或者其值为动作创建者的对象。

    ¥actionCreators (Function or Object): An action creator, or an object whose values are action creators.

  2. dispatch(功能):Store 实例上可用的 dispatch 功能。

    ¥dispatch (Function): A dispatch function available on the Store instance.

返回

¥Returns

(函数或对象):模仿原始对象的对象,但每个函数立即分派相应操作创建者返回的操作。如果你将函数作为 actionCreators 传递,则返回值也将是单个函数。

¥(Function or Object): An object mimicking the original object, but with each function immediately dispatching the action returned by the corresponding action creator. If you passed a function as actionCreators, the return value will also be a single function.

示例

¥Example

TodoActionCreators.js

export function addTodo(text) {
return {
type: 'ADD_TODO',
text
}
}

export function removeTodo(id) {
return {
type: 'REMOVE_TODO',
id
}
}

SomeComponent.js

import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import * as TodoActionCreators from './TodoActionCreators'
console.log(TodoActionCreators)
// {
// addTodo: Function,
// removeTodo: Function
// }

function TodoListContainer(props) {
// Injected by react-redux:
const { dispatch, todos } = props

// Here's a good use case for bindActionCreators:
// You want a child component to be completely unaware of Redux.
// We create bound versions of these functions now so we can
// pass them down to our child later.

const boundActionCreators = useMemo(
() => bindActionCreators(TodoActionCreators, dispatch),
[dispatch]
)
console.log(boundActionCreators)
// {
// addTodo: Function,
// removeTodo: Function
// }

useEffect(() => {
// Note: this won't work:
// TodoActionCreators.addTodo('Use Redux')

// You're just calling a function that creates an action.
// You must dispatch the action, too!

// This will work:
let action = TodoActionCreators.addTodo('Use Redux')
dispatch(action)
}, [])

return <TodoList todos={todos} {...this.boundActionCreators} />

// An alternative to bindActionCreators is to pass
// just the dispatch function down, but then your child component
// needs to import action creators and know about them.

// return <TodoList todos={todos} dispatch={dispatch} />
}

export default connect(state => ({ todos: state.todos }))(TodoListContainer)