I'v tried to use react-router and ReactTransitionGroup to make an navigation effect(page slide whereas route changes).
However, it's error-prone and ugly.(made much logic to define which direction to slide to and remove/add classes to make transition work).
I doubt is there any nice plugin to use.
Here's piece of my code, which inspired by Hardware-Accelerated Page Transitions for Mobile Web Apps / PhoneGap Apps.
const keyHistory = [];
let dir = 0;
const PageMixin = {
componentWillAppear(cb) {
keyHistory.push(this.props.location.key);
let $el = $(ReactDom.findDOMNode(this));
$el.addClass(pageStyles.right);
$el.one('transitionend', () => {
$el.removeClass(`${pageStyles.right} ${pageStyles.active}`);
cb();
});
requestAnimationFrame(() => {
$el.addClass(`${pageStyles.active} ${pageStyles.center}`);
});
},
componentWillEnter(cb) {
let key = this.props.location.key,
len = keyHistory.length;
if (key === keyHistory[len - 2]) {
keyHistory.pop();
dir = -1;
} else {
keyHistory.push(key);
dir = 1;
}
const fromDir = dir === -1 ? pageStyles.left : pageStyles.right;
let $el = $(ReactDom.findDOMNode(this));
$el.addClass(fromDir);
requestAnimationFrame(() => {
$el.removeClass(fromDir).addClass(`${pageStyles.active} ${pageStyles.center}`);
});
$el.one('transitionend', () => {
$el.removeClass(`${fromDir} ${pageStyles.active}`);
cb();
});
},
componentWillLeave(cb) {
const toDir = dir === -1 ? pageStyles.right : pageStyles.left;
let $el = $(ReactDom.findDOMNode(this));
requestAnimationFrame(() => {
$el.removeClass(pageStyles.center).addClass(`${pageStyles.active} ${toDir}`);
});
$el.one('transitionend', () => {
$el.removeClass(pageStyles.active);
cb();
});
}
};
Using links to switch pages js file, add the following code: import React, { Fragment } from "react"; import "./index. After importing Link , we have to update our navigation bar a bit. Now, instead of using a tag and href , React Router uses Link and to to, well, be able to switch between pages without reloading it.
The useHistory() hook is first imported and then assigned to a variable, which is subsequently utilized in a button (for example) to redirect users once a specific action is taken. Using the onClick event, we can then call the . push() method to tell React Router where we want the button to redirect to.
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.
import { Redirect } from "react-router-dom"; The easiest way to use this method is by maintaining a redirect property inside the state of the component. Whenever you want to redirect to another path, you can simply change the state to re-render the component, thus rendering the <Redirect> component.
You can try this
https://github.com/oliviertassinari/react-swipeable-views
Partial code from github
<SwipeableViews>
<div>
slide 1
</div>
<div>
slide 2
</div>
<div>
slide 3
</div>
</SwipeableViews>
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