react-router

Target Active Link when the route is active in Next.js

∥☆過路亽.° 提交于 2020-07-31 07:20:05
问题 How to target the active Link in Next.js like they way we do it in React-Router-4? Meaning, give the active link a class when its route is active? 回答1: First, you need to have a component called Link, with temporary attribute activeClassName import { withRouter } from 'next/router' import Link from 'next/link' import React, { Children } from 'react' const ActiveLink = ({ router, children, ...props }) => { const child = Children.only(children) let className = child.props.className || null if

React Pass history to a component defined in the Router

大城市里の小女人 提交于 2020-07-31 04:28:45
问题 I have this Router in my App.js: <Router basename={process.env.REACT_APP_ROUTER_BASE || '/MyApp'}> <Switch> <Route path="/" exact component={HomePage} /> <Route path="/login" component={Login} /> <Route path="/editProject" /*render={(props) => <ProjectEdit {...props} history={props.history} />}*/ component={ProjectEdit} /> {/*<Redirect path="*" to="/" />*/} </Switch> </Router> From HomePage component I'm using < ProjectsList> component which have < Project> components. Within the < Project>

How to access 'this.props.match.params' along with other props?

感情迁移 提交于 2020-07-21 07:28:25
问题 I have this code in my parent component: <Route path='/champions/:id' render={(props) => <ChampionDetails {...props} match={this.handleMatch}/>}/> where the match prop will be a function that retrieves data from the child component (ChampionDetails). A snippet of my ChampionDetails (child) component: import React, { Component } from 'react'; class ChampionDetails extends Component { state = { champId:this.props.match.params.id, champData:null, match:false } componentDidMount(){ console.log(

Prevent react-router history.push from reloading current route

浪子不回头ぞ 提交于 2020-07-18 04:05:13
问题 I'm taking my first steps with react-router . I'm currently using the hashHistory for development purposes and I'm performing 'manual' navigation. That is to say, I'm not using Link and I'm invoking history.push('/some/route'); in order to navigate (in response to plain old clicks on anchor tags). What I'm noticing is that, even when I'm already on the target route, react-router will re-render the relevant target component every time history.push('/target/route'); is invoked: On every push('

React Router v5.0 Nested Routes

倾然丶 夕夏残阳落幕 提交于 2020-07-17 11:22:06
问题 I'm building a react app and I can't make the routing work. I need one common layout (header, footer) for a few Auth routes ( /login , sign-up , forgot-password , etc...) And I need need another common layout for the rest of the protected parts of the app ( Home , Dashboard , etc...) I need another 404 page without any layout. I've tried several techniques from those links: Multiple Layouts with React Router v4 https://simonsmith.io/reusing-layouts-in-react-router-4 Nested routes with react

React: Hide a Component on a specific Route

寵の児 提交于 2020-07-17 10:16:41
问题 New to React: I have a <Header /> Component that I want to hide only when the user visit a specific page . The way I designed my app so far is that the <Header /> Component is not re-rendered when navigating , only the page content is, so it gives a really smooth experience. I tried to re-render the header for every route, that would make it easy to hide, but I get that ugly re-rendering glitch each time I navigate. So basically, is there a way to re-render a component only when going in and

react-router-dom Route paths defined behind '/' not working

我们两清 提交于 2020-07-10 11:25:33
问题 Im working on a proyect Using electron-react-boilerplate, react-hooks and Context. I have the issue on react-router-dom. Every path defined behind '/', navigation wont work, otherwhise it works properly. I tryed adding exact attribute on it, but first page stoped rendering Here is my code: Index.js: import React, { Fragment } from 'react'; import { render } from 'react-dom'; import './app.global.css'; import App from './App'; render(<App />, document.getElementById('root')); App.js: import

React: URL Profile ID doesn't match (match.params.id)

左心房为你撑大大i 提交于 2020-07-09 05:09:01
问题 So whenever I clicked the View Profile Link <Link to={`/profile/${_id}`} className="btn btn-primary"> View Profile </Link> It shows in the URL the User ID of the Profile which is Good. But whenever I Clicked it to Match it with this Button I get an error. const Profile = ({ getProfileById, match }) => { useEffect(() => { getProfileById(match.params.id); }, [getProfileById]); return <div>test</div>; }; I get on my console React Hook useEffect has a missing dependency: 'match.params.id'. Either

React Router - Open Link on new Tab and Redirect to Home Page

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-06 20:37:30
问题 Using React Router 4.2 My attempt is to open a new tab upon clicking on a navigation link and at the same time be redirected to the site homepage. ie: Navigation bar: clicking on Policies Even though the code bellow behaves as the requirements above: Is this the advisable way to go about it? Aiming to learn best practices here on Routes.js. //Routes.js import HandbookDoc from './policies.pdf' ... <Route path="/registration/policies" component={() => window.open(`${HandbookDoc}`,'_blank').then

React Router - Open Link on new Tab and Redirect to Home Page

最后都变了- 提交于 2020-07-06 20:35:47
问题 Using React Router 4.2 My attempt is to open a new tab upon clicking on a navigation link and at the same time be redirected to the site homepage. ie: Navigation bar: clicking on Policies Even though the code bellow behaves as the requirements above: Is this the advisable way to go about it? Aiming to learn best practices here on Routes.js. //Routes.js import HandbookDoc from './policies.pdf' ... <Route path="/registration/policies" component={() => window.open(`${HandbookDoc}`,'_blank').then