Using react-router I'm looking for a way to update the page URL / hash, without the router re-rendering the whole page.
I am developing a full page carousel, and would like each slide to have it's own URL (allowing the user to refresh the page and return to the correct slide). The carousel will later have swipe similar to this demo, which means the next slide is pre-rendered.
A stripped down version of my carousel is available here.
The current slide change looks like this:
onClickLeft: function() { this.setState({ selected: this.state.selected - 1 }); }
This works fine, with no URL updates. What I really want is:
mixin: [Navigation], onClickLeft: function() { this.transitionTo('carousel-slide', {num: this.state.selected + 1}); }
This would set the prop of the current slide, allowing the carousel to animate. However using this method now causes the page to re-render and no animation is displayed.
I have seen the ReactCSSTransitionGroup
used for route transitions, however this seems geared toward rendering a new page and transitioning out the old one.
If there's already a way to achieve what I'm looking for, and I've missed it, could someone point me in the right direction?
To use React Router without changing the URL, we can use the MemoryRouter component. to import the MemoryRouter component and then wrap it around App to let us navigate with React Router without changing URLs.
Using react-router-dom to Change URL Path and Render Components. The react-router-dom package is great for rendering different React components based on the url path. Therefore, React components can lead to others by changing the url path.
react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.
react-router no longer sets a key on your routes. If you do need to set a key from a route handler, put it on a surrounding element.
return ( <div key={this.props.params.bookId}> {this.props.children} </div> );
It's now <ReactRouter.RouteHandler key="anything" />
, but this is also no longer really needed due to changes in react-router. See the changelog for more details.
Currently, react-router sets a key on your handler based on the current route. When react does its diff, and notices a different key, it throws out the entire subtree both in virtual and real dom, and rerenders.
To prevent this, you can override react-router's key when using activeRouteHandler()
this.props.activeRouteHandler({key: "anything"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With