警告:<span>标记上的prop"dispatch"值无效

Vad*_*dim 7 reactjs react-redux

我已经阅读了关于新变化的博客(在哪里描述了这样的警告),所以我有一个问题:编写纯组件的正确方法是什么,而不使用任何操作?

以下是此错误的示例

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;
Run Code Online (Sandbox Code Playgroud)

如果我使用connect将Text连接到商店 - 我将会出现此错误,因为我没有在mapDispatchToProps函数中编写任何内容并根据文档:"如果您不提供自己的mapDispatchToProps函数或对象充满动作创建者,则默认mapDispatchToProps实现只是将调度注入到组件的道具中."

所以我有一个选择:

to declare dispatch in props in dumb component and omit it in params in Text rendering
to write fake mapDispatchToProps function in connect
Run Code Online (Sandbox Code Playgroud)

哪种变体更可取?

Dmi*_*nko 5

您没有从道具传播到 Tag

const Text = ({
    tagName = 'span',
    className = '',
    children = null,
    dispatch
    ...restProps
})
Run Code Online (Sandbox Code Playgroud)