簡體   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