ReactJS Link tag - Uncaught ReferenceError: Link is not defined
Asked Answered
L

4

14

How do I add <a> tag correctly? I get this error for adding <Link></Link>

Uncaught ReferenceError: Link is not defined

The code:

   render() {
        return (
            <div>
                <Link className="button-close-overlay"><span className="glyphicon glyphicon-remove"></span></Link>
                <article>
                    <div className="container">
                        <div className="content align-center" dangerouslySetInnerHTML={{__html: this.state.article.content}}></div>
                    </div>
                </article>
            </div>
        );
    }

The entire code:

class Article extends React.Component
{
    constructor(props) {
        super(props);
        this.state = {
            article: [],
        };
    }

    // Then fetch the data using $.get():
    componentDidMount() {
        this.serverRequest = $.get(this.props.source, function (result) {
            this.setState({
                article: result
            });
        }.bind(this));
    }

    componentWillUnmount() {
        this.serverRequest.abort();
    }

    render() {
        return (
            <div>
                <link className="button-close-overlay"><span className="glyphicon glyphicon-remove"></span></link>
                <article>
                    <div className="container">
                        <div className="content align-center" dangerouslySetInnerHTML={{__html: this.state.article.content}}></div>
                    </div>
                </article>
            </div>
        );
    }
}

export { Article as default }

Any ideas?

Led answered 13/10, 2016 at 8:43 Comment(0)
B
22

If you're thinking of the React router Link. Then you need to import it.

import { Link } from 'react-router';

or

var Link = require('react-router').Link

otherwise rendering a pure <a> does the trick!

Bedad answered 13/10, 2016 at 9:8 Comment(2)
Aww I see! will fallback to <a> which is better! thanks! :-)Led
@HenrikAndersson This import from react-router does not work in react-router 4+, you must import from react-router-dom insteadFare
S
19

another option would be

import {Link} from 'react-router-dom';
Societal answered 1/11, 2017 at 3:8 Comment(0)
H
2

If you use Nextjs, you can also use Link from next/link

import Link from "next/link"
Herr answered 23/12, 2019 at 2:38 Comment(1)
This just work. github.com/zeit/next-learn-demo/blob/master/…Tutu
B
0

Sometimes this error shows up when you are not importing React in the same page, try doing so at the top of the file:

import { Link } from "react-router-dom";
import React from "react";
Bookman answered 27/4 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.