Ad
React Router Link With Params
I have a route like this: CODE FOR MY ROUTER
import React from 'react';
import ErrorBoundary from './errorBoundary/errorPage';
import Home from './homePage';
import NotFoundPage from './notFoundPage';
import About from './about/aboutPage';
import Authors from './authors/authorPage';
import ManageAuthorPage from './authors/manageAuthorPage';
import { Switch, Route, Redirect, Router, NotFoundRoute } from 'react-router-dom';
class MyRouter extends React.Component{
render(){
return(
<div>
<Switch>
<Route exact path="/app" name="app" component={Home}/>
<Route exact path="/" component={Home}/>
<Route path="/author" name="author" component={Authors} />
<Route path="/about" name="about" component={About}/>
<Route name="manageAuthor" path="/manageAuthor/:id" component={ManageAuthorPage} />
<Route name="addAuthor" path="/addAuthor" component={ManageAuthorPage} />
<Redirect from="/about/*" to="/about" />
<Route component={NotFoundPage} />
</Switch>
</div>
)
}
}
export default MyRouter;
and in my component i have a link that looks like this:
"use strict";
import React from 'react';
import propTypes from 'prop-types';
import { Link } from 'react-router-dom';
import Router from 'react-router-dom';
class AuthorList extends React.Component{
componentDidCatch(error, info) {
console.log(error + " - " + info);
}
render(){
var createAuthorRow = function(author){
return (
<tr key={ author.id }>
<td><Link to={`/manageAuthor/${ author.id }`}>{ author.id }</Link></td>
<td>{ author.firstName } { author.lastName }</td>
</tr>
);
}
return (
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
</tr>
</thead>
<tbody>
{ this.props.authors.map(createAuthorRow, this) }
</tbody>
</table>
);
}
}
AuthorList.propTypes = {
authors : propTypes.array.isRequired
};
AuthorList.defaultProps = {
authors: []
};
export default AuthorList;
upon clicking the specific author PICTURE OF MY PAGE
it redirects on this page PAGE FOR AUTHOR DETAILS
which is working fine. However when i click other link from the navigation, the url became like this http://localhost:2345/manageAuthor/author supposedly it will look like this http://localhost:2345/author. What's wrong with my code. Please help me figure out. Thank You.
"react": "^16.3.1",
"react-dom": "^16.3.1",
"react-redux": "^5.0.7",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2"
code for my navigation
<ul className="nav navbar-nav">
<li className="nav-item">
<Link className="nav-link" to="app"> Dashboard </Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="author"> Authors </Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="about"> About </Link>
</li>
</ul>
Ad
Answer
Update you navbar to use absolute URLs instead of relative ones:
<ul className="nav navbar-nav">
<li className="nav-item">
<Link className="nav-link" to="/app"> Dashboard </Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/author"> Authors </Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/about"> About </Link>
</li>
</ul>
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad