Created
May 26, 2019 09:42
-
-
Save BrychanOdlum/902e894d334bcdfa9bef3c6e68a7c6af to your computer and use it in GitHub Desktop.
React router pseudo route hoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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