Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router Error related to Link To

I am trying to route the components from one to another, i am getting errors related to the react router link and the error for the react router is - Failed to compile i have tried using react router and added its package to my project library.

here are code for the what i made for doing the route:-
1) [Index.js file][2]
2) [Navlink.js file][3]
3) [nav.js file][4]

In Index.js i have made a router in render method but it shows errors above.
In Navlink.js i have made a link return class which provide the link and all properties.
In nav.js file i have used the navlink by import navlink.js file. but the errors keeps coming related to that link does'nt exist in react router and hashHistory does'nt exist in react router.

If anyone Knows Please help.

1)Index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, hashHistory } from 'react-router';
import './index.css';
import App from './App';
import Homecontent from './homepage';
import Foo from './footer';
import registerServiceWorker from './registerServiceWorker';
import ourAdvantages from './ouradvantage';
import LoginForm from './loginform';
import BoRegistration from './boregistration';
import VoRegistration from './voregistration';

ReactDOM.render(<App />, document.getElementById('header'));
registerServiceWorker();
// ReactDOM.render(<Homecontent />, document.getElementById('homecontent'));
ReactDOM.render((
    <Route path="/" component={App}>
      <Route path="/loginform" component={LoginForm}>
      </Route>
    </Route>
), document.getElementById('homecontent'))
//ReactDOM.render(<LoginForm />, document.getElementById('loginform'));
//ReactDOM.render(<BoRegistration />, document.getElementById('boRegistration'));
//ReactDOM.render(<VoRegistration />, document.getElementById('voRegistration'));
//ReactDOM.render(<ourAdvantages />, document.getElementById('ouradvantages'));
ReactDOM.render(<Foo />, document.getElementById('footer'));

2) navLink.js

import React from 'react'
import { Link } from 'react-router'

export default React.createClass({
  render() {
    return <Link {...this.props} activeClassName="active"/>
  }
})

3) nav.js

class NavMenu extends Component {
  render(){
    return (
      <ul className="navbar-nav">
      <li className="nav-item">
        <navLink to="/loginform/reactjs/react-router"><a className="nav-link" href="#">Home</a></navLink>
      </li>
      <li className="nav-item">
        <a className="nav-link" href="#">Our Advantages</a>
      </li>
      <li className="nav-item">
        <a className="nav-link" href="#">Offers</a>
      </li>
like image 963
Husain Khanbahadur Avatar asked Sep 04 '17 13:09

Husain Khanbahadur


People also ask

What does link do in React router?

A <Link> is an element that lets the user navigate to another page by clicking or tapping on it. In react-router-dom , a <Link> renders an accessible <a> element with a real href that points to the resource it's linking to. This means that things like right-clicking a <Link> work as you'd expect.

How do I fix error 404 in React?

Use a wildcard placeholder to handle 404 page not found in React router, e.g. <Route path="*" element={<PageNotFound />} /> . A route that has an asterisk path * serves as a catch all route. It only matches when no other routes do.

How do I add links to my React router?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.


1 Answers

I hope you are using react-router 4x. In these new version you have to import Link from react-router-dom.

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

You can check the same here https://reacttraining.com/react-router/web/api/Link.

But i hope you are following some tutorial or still using react-router 3.x style. So either you have to use correct version of react-router or follow the correct style of writing according to the version.

like image 85
Panther Avatar answered Oct 09 '22 11:10

Panther