Skip to content

Instantly share code, notes, and snippets.

@BrychanOdlum
Created May 26, 2019 09:42
Show Gist options
  • Save BrychanOdlum/902e894d334bcdfa9bef3c6e68a7c6af to your computer and use it in GitHub Desktop.
Save BrychanOdlum/902e894d334bcdfa9bef3c6e68a7c6af to your computer and use it in GitHub Desktop.
React router pseudo route hoc
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';
import { setPseudoRoute } from '../utils/Utils';
function withPseudoRoute(Component, deeplink) {
return withRouter(props => {
useEffect(() => {
setPseudoRoute(deeplink);
return () => {
if (window.location.pathname === deeplink) {
setPseudoRoute(props.location.pathname);
}
}
}, []);
return <Component {...props} />;
});
}
withPseudoRoute.propTypes = {
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]),
};
withPseudoRoute.defaultTypes = {
children: null,
};
export default withPseudoRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment