将react-router-dom Link组件作为渲染道具传递失败

时间:2019-04-19 16:27:40

标签: reactjs react-router-dom

我正在尝试将react-router-dom Link组件作为渲染道具传递给组件,但是遇到了一些问题:

https://codesandbox.io/embed/xjm35923qo

通过<Link>hi</Link作为渲染道具时收到的错误是Invariant Violation Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

这似乎很公平,但是如果我改为传递诸如() => <Link>hi</Link>之类的内容,则没有错误,但是有警告(react-dom.development.js?7f13:507 Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.)并且链接不会呈现。

我无法解决这些错误消息-我在这里想念什么?

1 个答案:

答案 0 :(得分:2)

您应将Link导入为命名导入:

import {Link} from "react-router-dom"

,您需要使用Router对象包装您的应用。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

相关问题