Skip to the content.

connectAdvanced()

connectAdvanced(selectorFactory, connectOptions?)

React组件连接到Redux存储。它是connect()的基础,但对于如何将状态、propsdispatch组合到最终的props中并不那么固执。它对默认值或结果记忆没有任何假设,将这些责任留给调用者。

Connects a React component to a Redux store. It is the base for connect() but is less opinionated about how to combine state, props, and dispatch into your final props. It makes no assumptions about defaults or memoization of results, leaving those responsibilities to the caller.

它不会修改传递给它的组件类;相反,它返回一个新的、连接的组件类供您使用。

It does not modify the component class passed to it; instead, it returns a new, connected component class for you to use.

大多数应用程序不需要使用这个,因为connect中的默认行为适用于大多数用例。

Most applications will not need to use this, as the default behavior in connect is intended to work for most use cases.

注意 connectAdvanced是在5.0版本中添加的,connect作为connectAdvanced的特定参数集重新实现。然而,connectAdvanced现在已弃用,并将在未来的React Redux主版本中被删除。

connectAdvanced was added in version 5.0, and connect was reimplemented as a specific set of parameters to connectAdvanced. However, connectAdvanced is now deprecated and will eventually be removed in a future major version of React Redux.

Arguments

Returns

一个更高阶的React组件类,它从存储状态构建props,并将它们传递给包装好的组件。高阶组件是一个函数,它接受一个组件参数并返回一个新组件。

A higher-order React component class that builds props from the store state and passes them to the wrapped component. A higher-order component is a function which accepts a component argument and returns a new component.

Static Properties

Static Methods

组件的所有原始静态方法都被提升。

All the original static methods of the component are hoisted.

Remarks

案例

根据道具注入特定用户的待办事项,并注入道具。userId转换到操作中

Inject todos of a specific user depending on props, and inject props.userId into the action

import * as actionCreators from './actionCreators'
import { bindActionCreators } from 'redux'

function selectorFactory(dispatch) {
  let ownProps = {}
  let result = {}

  const actions = bindActionCreators(actionCreators, dispatch)
  const addTodo = (text) => actions.addTodo(ownProps.userId, text)

  return (nextState, nextOwnProps) => {
    const todos = nextState.todos[nextOwnProps.userId]
    const nextResult = { ...nextOwnProps, todos, addTodo }
    ownProps = nextOwnProps
    if (!shallowEqual(result, nextResult)) result = nextResult
    return result
  }
}
export default connectAdvanced(selectorFactory)(TodoApp)