gpt4 book ai didi

reactjs - 警告 : Invalid value for prop `dispatch` on tag

转载 作者:行者123 更新时间:2023-12-03 13:26:01 25 4
gpt4 key购买 nike

我读过有关新更改的博客(在哪里描述此类警告),所以我有一个问题:编写不使用任何操作的纯组件的正确方法是什么?

这是一个出现此错误的示例

const Text = ({
tagName = 'span',
className = '',
children = null,
...restProps
}) => {
const Tag = tagName;

return (
<Tag {...restProps} className={className}>
{children}
</Tag>
);
};

Text.defaultProps = {
tagName: 'span',
className: '',
children: null,
};

export default Text;

如果我使用 connect 将 Text 连接到商店 - 我将出现此错误,因为我在 mapDispatchToProps 函数中没有任何内容可写,并且根据文档:“如果您不提供自己的mapDispatchToProps函数或充满 Action 创建者的对象,则默认的mapDispatchToProps实现只会将调度注入(inject)组件的 Prop 中。”

所以我有一个选择:

to declare dispatch in props in dumb component and omit it in params in Text rendering
to write fake mapDispatchToProps function in connect

哪种变体更理想?

最佳答案

您没有从传递给 Tag 的 props 中传播调度

const Text = ({
tagName = 'span',
className = '',
children = null,
dispatch,
...restProps
})

关于reactjs - 警告 : Invalid value for prop `dispatch` on <span> tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com