简体   繁体   English

Redux connect 将不必要的道具“调度”传递给连接的组件

[英]Redux connect passes an unnecessary prop "dispatch" to a connected component

I'm connecting store to the Image component using redux connect.我正在使用 redux connect 将商店连接到 Image 组件。 I only expect to retrieve there (in the Image component) one prop from Redux: "images".我只希望在那里(在 Image 组件中)从 Redux 中检索一个道具:“images”。 But I also get an additional prop: "dispatch".但我也得到了一个额外的道具:“调度”。

import { connect } from "react-redux";
import Image from "./Image";
import { imagesSelector } from "units/images/selectors";

const mapStateToProps: MapStateToPropsParam = (state) => ({
  images: imagesSelector(state),
});

export default connect(mapStateToProps)(Image);

In the Image component I get: { images: [...], dispatch: function(){...}}在 Image 组件中,我得到: { images: [...], dispatch: function(){...}}

The question: How to exclude dispatch from being passed to my component?问题:如何从传递给我的组件中排除调度?

From the react-redux docs:来自 react-redux 文档:

If you don't specify a second argument to connect() , your component will receive dispatch as a prop by default.如果你没有为connect()指定第二个参数,你的组件将默认接收dispatch作为 prop。

If you don't want this behavior, you can instead pass a second argument to the connect function, which would be the mapDispatchToProps argument.如果您不想要这种行为,您可以改为将第二个参数传递给connect函数,这将是mapDispatchToProps参数。

It can be either a function or an object.它可以是一个函数,也可以是一个对象。

We recommend always using the “object shorthand” form of mapDispatchToProps, unless you have a specific reason to customize the dispatching behavior.我们建议始终使用 mapDispatchToProps 的“对象简写”形式,除非您有特定的理由来自定义调度行为。

Note that:注意:

  • Each field of the mapDispatchToProps object is assumed to be an action creator mapDispatchToProps 对象的每个字段都被假定为一个动作创建者
  • Your component will no longer receive dispatch as a prop您的组件将不再作为道具接收调度

Docs: https://react-redux.js.org/using-react-redux/connect-mapdispatch文档: https : //react-redux.js.org/using-react-redux/connect-mapdispatch

export default connect(mapStateToProps, null)(Image);

添加 null 作为第二个参数,你就可以开始了

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

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