繁体   English   中英

react-router链接不起作用

[英]react-router Link is not working

我试图在反应中创建一个简单的链接。 这是代码:

    import React from 'react';
import { Link }  from 'react-router';
class List extends React.Component{
    render() {
        return (
        <div>
        <p>please choose from repository below.</p>
        <ul>
            <li><Link to={"/react"}>React</Link></li>
        </ul>
        </div>
        );
    }
}

export default List;

这是我的package.json:

"dependencies": {
    "chance": "^1.0.9",
    "history": "^4.6.1",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-router": "^4.1.1",
    "react-router-dom": "^4.1.1",
    "superagent": "^3.5.2"

我使用以下命令来运行我的代码:

的WebPack-DEV-服务器

bundle.js:1004 Warning: React.createElement: 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. Check the render method of `List`.
    in List (created by Route)
    in Route
    in div
    in Router (created by HashRouter)
    in HashRouter

如果我删除

<li><Link to={"/react"}>React</Link></li> 

从我的代码然后页面正确加载。 我是否使用在组件上创建链接的旧方法?

任何提示都表示赞赏......

Link现在是react-router-dom包的一部分。 所以你应该将导入更改为:

import { Link } from 'react-router-dom';

您需要从react-router-dom导入Link ,您可以在此处查看示例。 此外,不需要使用括号,您只需执行以下操作: <Link to="/react">React</Link>

暂无
暂无

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

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