react-router-v4

React Router with optional path parameter

蹲街弑〆低调 提交于 2019-11-26 16:56:36
I want to declare a path with an optional path parameter, hence when I add it the page to do something extra (e.g. fill some data): http://localhost/app/path/to/page <= render the page http://localhost/app/path/to/page/pathParam <= render the page with some data according to the pathParam In my react router I have the following paths, in order to support the two options (this is a simplified example): <Router history={history}> <Route path="/path" component={IndexPage}> <Route path="to/page" component={MyPage}/> <Route path="to/page/:pathParam" component={MyPage}/> </Route> </Router> My

onEnter not called in React-Router

混江龙づ霸主 提交于 2019-11-26 15:42:28
问题 Ok, I'm fed up trying. The onEnter method doesn't work. Any idea why is that? // Authentication "before" filter function requireAuth(nextState, replace){ console.log("called"); // => Is not triggered at all if (!isLoggedIn()) { replace({ pathname: '/front' }) } } // Render the app render( <Provider store={store}> <Router history={history}> <App> <Switch> <Route path="/front" component={Front} /> <Route path="/home" component={Home} onEnter={requireAuth} /> <Route exact path="/" component=

How to listen to route changes in react router v4?

眉间皱痕 提交于 2019-11-26 15:20:39
问题 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? 回答1: 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

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

泪湿孤枕 提交于 2019-11-26 14:37:23
问题 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? 回答1: Instead of using BrowserRouter you could use the Router with custom history

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

五迷三道 提交于 2019-11-26 14:18:36
问题 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

React doesn&#39;t reload component data on route param change or query change

落花浮王杯 提交于 2019-11-26 13:45:16
I have a "home" component with links, and when you click a link the product component is loaded with the product. I also have another component which is always visible, showing links to the "recently visited products". These links don't work when on a product page. The url updates when I click the link, and a render occurs, but the product component doesn't update with the new product. See this example: Codesandbox example Here are the routes in index.js: <BrowserRouter> <div> <Route exact path="/" render={props => <Home products={this.state.products} />} /> <Route path="/products/:product"

How to nest routes in React Router v4?

回眸只為那壹抹淺笑 提交于 2019-11-26 13:07:39
问题 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

Nested Routes not rendering with React Router v4

醉酒当歌 提交于 2019-11-26 12:31:57
问题 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

Detect Route Change with react-router

南楼画角 提交于 2019-11-26 12:04:03
问题 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? 回答1: 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

Programmatically navigating in React-Router v4

徘徊边缘 提交于 2019-11-26 11:53:17
How can I move to a new page after some validation is done with React Router V4 ? I have something like this: export class WelcomeForm extends Component { handleSubmit = (e) => { e.preventDefault() if(this.validateForm()) // send to '/life' } render() { return ( <form className="WelcomeForm" onSubmit={this.handleSubmit}> <input className="minutes" type="number" value={this.state.minutes} onChange={ (e) => this.handleChanges(e, "minutes")}/> </form> ) } } I would like to send the user to another route. I had a look at Redirect but it seems like it would delete the current page from the history