【问题标题】:react-router Link is not working反应路由器链接不起作用
【发布时间】:2017-06-13 20:42:39
【问题描述】:

我正在尝试在 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-server

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> 

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

感谢任何提示...

【问题讨论】:

    标签: reactjs react-router react-redux


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您需要从react-router-dom 导入Link,您可以查看示例here。也不需要使用括号,你可以简单地做:&lt;Link to="/react"&gt;React&lt;/Link&gt;

      【讨论】:

        猜你喜欢
        • 2017-03-06
        • 2021-09-11
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多