[英]react router component props doesn't accept <Link /> component?
What's wrong with this? 这怎么了
import { Switch, Route, Link } from 'react-router'
<Route path='/' component={() => {
return (
<div>
<Link to='/'>Home</Link>
<Link to='/users'>Users</Link>
</div>
)
}}
/>
I got error of 我有错误
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义
but if I just do this it's fine 但是如果我这样做就可以了
<Route path='/' component={() => {
return (
<div>
Home
</div>
)
}}
/>
Link
component is not an export in react-router
package but react-router-dom
Link
组件不是react-router
包中的导出,而是react-router-dom
You need to install it using 您需要使用安装
npm install -S react-router-dom
and then use it like 然后像
import { Link } from 'react-router-dom';
<Route path='/' render={(props) => {
return (
<div>
<Link to='/'>Home</Link>
<Link to='/users'>Users</Link>
</div>
)
}}
/>
Also when using a functional component inline in Route
make use of render
and not component
prop with the functional argument as props 另外,在使用功能部件直列当
Route
利用render
,而不是component
支柱与功能参数作为道具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.