Skip to content

Instantly share code, notes, and snippets.

@stephencookdev
Created July 16, 2018 15:09
Show Gist options
  • Save stephencookdev/cc7177c8fd0663fc0050a36f0d81ef7c to your computer and use it in GitHub Desktop.
Save stephencookdev/cc7177c8fd0663fc0050a36f0d81ef7c to your computer and use it in GitHub Desktop.
`SwitchWithSlide` example
export const animateSwitch = (CustomSwitch, AnimatorComponent) => ({
children,
}) => (
<Route
render={({ location }) => (
<AnimatorComponent uniqKey={location.pathname}>
<CustomSwitch location={location}>{children}</CustomSwitch>
</AnimatorComponent>
)}
/>
);
import React from "react";
import { animateSwitch } from "./animateSwitch";
import { SlideOut } from "./SlideOut";
const SwitchWithSlide = animateSwitch(Switch, SlideOut);
export default () => (
<SwitchWithSlide>
<Route path="/a" component={PageA} />
<Route path="/b" component={PageB} />
</SwitchWithSlide>
);
export class SlideOut extends React.Component {
constructor(props) {
super(props);
this.state = {
childPosition: Slider.CENTER,
curChild: props.children,
curUniqId: props.uniqId,
prevChild: null,
prevUniqId: null,
animationCallback: null
};
}
componentDidUpdate(prevProps, prevState) {
const prevUniqId = prevProps.uniqKey || prevProps.children.type;
const uniqId = this.props.uniqKey || this.props.children.type;
if (prevUniqId !== uniqId) {
this.setState({
childPosition: Slider.TO_LEFT,
curChild: this.props.children,
curUniqId: uniqId,
prevChild: prevProps.children,
prevUniqId,
animationCallback: this.swapChildren
});
}
}
swapChildren = () => {
this.setState({
childPosition: Slider.FROM_RIGHT,
prevChild: null,
prevUniqId: null,
animationCallback: null
});
};
render() {
return (
<Slider
position={this.state.childPosition}
animationCallback={this.state.animationCallback}
>
{this.state.prevChild || this.state.curChild}
</Slider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment