react-router链接不起作用

时间:2017-06-13 20:42:40

标签: reactjs react-router react-redux

我正在尝试创建一个简单的Link in react。这是代码:

    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> 
从我的代码

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

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

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

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