react-router-v4

Using Jest to test a Link from react-router v4

你离开我真会死。 提交于 2019-11-28 06:22:08
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 = component.toJSON(); expect(tree).toMatchSnapshot(); }); The warning when the test is run: Warning: Failed

react router with dynamic routes give 404 on a browser direct link

喜夏-厌秋 提交于 2019-11-28 05:34:18
问题 I have a React Front-end connected to Wordpress API and the routes are defined in clientside with React Router . When i use Link to direct them to dynamic routes they work fine. But when i use browser address bar directly to access the links pressing enter, or i refresh.i get 404 page not found and i got to know that is because a direct call to server is happening and the there's no javascript to manipulate the react-router actions on the shared host when we have a PHP Server on that. Thought

React history.push() is updating url but not navigating to it in browser

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 20:43:08
问题 I've read many things about react-router v4 and the npm history library to this point, but none seems to be helping me. My code is functioning as expected up to the point when it should navigate and do a simple redirect when the url is changed using the history.push() method. The URL IS changing to the specified route, but not doing the redirect on the button push. Thanks in advance, still learning react-router... I would like for the button push to do a simple redirect without the

react-router (v4) how to go back?

拟墨画扇 提交于 2019-11-27 20:02:05
Trying to figure out how can I go back to the previous page. I am using [react-router-v4][1] This is the code I have configured in my first landing page: <Router> <div> <Link to="/"><div className="routerStyle"><Glyphicon glyph="home" /></div></Link> <Route exact path="/" component={Page1}/> <Route path="/Page2" component={Page2}/> <Route path="/Page3" component={Page3}/> </div> </Router> In order to forward to subsequent pages, I simply do: this.props.history.push('/Page2'); However, how can I go back to previous page? Tried few things like mentioned below but no luck: 1. this.props.history

Why can I not nest Route components in react-router 4.x?

会有一股神秘感。 提交于 2019-11-27 17:52:09
问题 How in the world does one use nested routes in react-router, specifically, version 4.x? The following worked well in previous versions... <Route path='/stuff' component={Stuff}> <Route path='/stuff/a' component={StuffA} /> </Route> Upgrading to 4.x throws the following warning... Warning: You should not use <Route> component and <Route children> in the same route; <Route children> will be ignored What in the heck is going on here? I've scoured the docs for hours and can not successfully get

react-router 4 - Browser history needs a DOM

≯℡__Kan透↙ 提交于 2019-11-27 13:43:07
问题 I am trying server side rendering using react-router 4. I am following the example provided here https://reacttraining.com/react-router/web/guides/server-rendering/putting-it-all-together As per the example on server we should use StaticRouter . When I import as per the example I am seeing StaticRouter as undefined import {StaticRouter} from 'react-router'; After doing some research online I found I could use react-router-dom . Now my import statement looks like this. import {StaticRouter}

Detect Route Change with react-router

断了今生、忘了曾经 提交于 2019-11-27 11:07:10
I have to implement some business logic depending on browsing history. What I want to do is something like this: reactRouter.onUrlChange(url => { this.history.push(url); }); Is there any way to receive a callback from react-router when the URL gets updated? You can make use of history.listen() function when trying to detect the route change. Considering you are using react-router v4 , wrap your component with withRouter HOC to get access to the history prop. history.listen() returns an unlisten function. You'd use this to unregister from listening. You can configure your routes like index.js

How to listen to route changes in react router v4?

喜欢而已 提交于 2019-11-27 10:54:22
I have a couple of buttons that acts as routes. Everytime the route is changed, I want to make sure the button that is active changes. Is there a way to listen to route changes in react router v4? I use withRouter to get the location prop. When the component is updated because of a new route, I check if the value changed: @withRouter class App extends React.Component { static propTypes = { location: React.PropTypes.object.isRequired } // ... componentDidUpdate(prevProps) { if (this.props.location !== prevProps.location) { this.onRouteChanged(); } } onRouteChanged() { console.log("ROUTE CHANGED

Use history.push in action creator with react-router-v4?

匆匆过客 提交于 2019-11-27 09:14:30
The only working method that I found to work this out without using react-router-redux to route from action creator async action completion is by passing the history prop from the component to action creator and doing: history.push('routename'); Since BrowserRouter ignores the history prop passed to it thus we cannot use custom history objects with browserhistory. What is the best possible way to work this out? Instead of using BrowserRouter you could use the Router with custom history like import { Router } from 'react-router' import createBrowserHistory from 'history/createBrowserHistory'

React router 4 does not update view on link, but does on refresh

孤街浪徒 提交于 2019-11-27 08:49:04
I am using the following simple nav code <Router> <Switch> <Route exact path='/dashboard' component={Dashboard} /> <Route path='/dashboard/accounts' component={AccountPage} /> </Switch> </Router> <NavLink exact to={'/dashboard'} disabled={this.props.item.disabled} activeClassName='active'> <NavLink exact to={'/dashboard/accounts'} disabled={this.props.item.disabled} activeClassName='active'> The URL changes but the view does not. It does however change when I refresh the page or manually go to that URL. You can also use the: import { withRouter } from 'react-router-dom'; And then on your