react-router-v4

How to nest routes in React Router v4?

夙愿已清 提交于 2019-11-27 07:27:55
Is there a way to nest routes in React Router v4? This works: <Router basename='/app'> <main> <Route path='/' component={AppBar} /> <Route path='/customers' component={Customers} /> </main> </Router> This does not: <Router basename='/app'> <Route path='/' component={AppBar}> <Route path='/customers' component={Customers} /> </Route> </Router> Customers Component: import React, { Component, PropTypes } from 'react' import styled from 'styled-components' export default class Customers extends Component { render () { return ( <Container> <h1>Customers</h1> </Container> ) } } const Container =

Detect change in query param react-router-dom v4.x and re-render component

亡梦爱人 提交于 2019-11-27 05:34:48
I am not really sure why its showing the default route once I did a query param change. Is there a better approach for this kind of issue? maybe I shouldn't be using query param? Open to get educated! Version "react": "^16.2.0", "react-dom": "^16.2.0", "react-router": "^4.2.0", "react-router-dom": "^4.2.2", Test Case https://codepen.io/adamchenwei/pen/YeJBxY?editors=0011 Steps to reproduce Click on Home -> Step 1 Expected Behavior Go to Step 1 as well as Step 2 render correct dom Actual Behavior Empty, nothing renders on the page // For this demo, we are using the UMD build of react-router-dom

How to get query parameters in react-router v4

安稳与你 提交于 2019-11-27 05:11:17
问题 I'm using react-router-dom 4.0.0-beta.6 in my project. I have a code like following: <Route exact path="/home" component={HomePage}/> And I want to get query params in HomePage component. I've found location.search param, which looks like this: ?key=value , so it is unparsed. What is the right way to get query params with react-router v4? 回答1: The ability to parse query strings was taken out of V4 because there have been requests over the years to support different implementation. With that,

Nested Routes in React Router v4

。_饼干妹妹 提交于 2019-11-27 03:10:30
I'm trying to set up some nested routes to add a common layout. Check the code out: <Router> <Route component={Layout}> <div> <Route path='/abc' component={ABC} /> <Route path='/xyz' component={XYZ} /> </div> </Route> </Router> While this works perfectly fine, I still get the warning: Warning: You should not use <Route component> and <Route children> in the same route; will be ignored CESCO's answer renders first the component AppShell then one of the components inside Switch . But these components are NOT going to render inside AppShell , they will NOT be children of AppShell . In v4 to wrap

Nested Routes not rendering with React Router v4

梦想的初衷 提交于 2019-11-27 02:07:21
I am trying to group some of my routes together with React Router v4 to clean up some of my components. For now I just want to have my non logged in routes group together and my admin routes grouped together but the following doens't work. main.js const Main = () => { return ( <main> <Switch> <Route exact path='/' component={Public} /> <Route path='/admin' component={Admin} /> </Switch> </main> ); }; export default Main; public.js const Public = () => { return ( <Switch> <Route exact path='/' component={Greeting} /> <Route path='/signup' component={SignupPage} /> <Route path='/login' component

Moving from react router 3.x to 4.x

倖福魔咒の 提交于 2019-11-27 01:21:32
问题 How can I move to using https://cdnjs.cloudflare.com/ajax/libs/react-router/4.0.0-2/react-router.min.js from using https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.1/ReactRouter.min.js ? Example using 3.x below. HTML <script src="https://unpkg.com/react@15.4.2/dist/react.min.js"></script> <script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.1/ReactRouter.min.js"></script> JS let { Router,

Using Jest to test a Link from react-router v4

给你一囗甜甜゛ 提交于 2019-11-27 01:18:16
问题 I'm using jest to test a component with a <Link> from react-router v4. I get a warning that <Link /> requires the context from a react-router <Router /> component. How can I mock or provide a router context in my test? (Basically how do I resolve this warning?) Link.test.js import React from 'react'; import renderer from 'react-test-renderer'; import { Link } from 'react-router-dom'; test('Link matches snapshot', () => { const component = renderer.create( <Link to="#" /> ); let tree =

React Router work on reload, but not when clicking on a link

北战南征 提交于 2019-11-26 22:54:05
问题 I have setup the React with react-router version 4. The routing works when I enter the URL directly on the browser, however when I click on the link, the URL changes on the browser (e.g http://localhost:8080/categories), but the content don't get updated (But if I do a refresh, it gets updated). Below is my setup: The Routes.js setup as follows: import { Switch, Route } from 'react-router-dom'; import React from 'react'; // Components import Categories from './containers/videos/Categories';

React Router v4 - How to get current route?

爷,独闯天下 提交于 2019-11-26 20:18:19
I'd like to display a title in <AppBar /> that is somehow passed in from the current route. In React Router v4, how would <AppBar /> be able to get the current route passed into it's title prop? <Router basename='/app'> <main> <Menu active={menu} close={this.closeMenu} /> <Overlay active={menu} onClick={this.closeMenu} /> <AppBar handleMenuIcon={this.handleMenuIcon} title='Test' /> <Route path='/customers' component={Customers} /> </main> </Router> Is there a way to pass a custom title from a custom prop on <Route /> ? Con Posidielov In react router 4 the current route is in - this.props

How to implement authenticated routes in React Router 4?

偶尔善良 提交于 2019-11-26 16:57:08
I was trying to implement authenticated routes but found that React Router 4 now prevents this from working: <Route exact path="/" component={Index} /> <Route path="/auth" component={UnauthenticatedWrapper}> <Route path="/auth/login" component={LoginBotBot} /> </Route> <Route path="/domains" component={AuthenticatedWrapper}> <Route exact path="/domains" component={DomainsIndex} /> </Route> The error is: Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored In that case, whats the correct way to implement this? It appears in react