NavBarButton reacts to hash changes

This commit is contained in:
NikolaBorislavovHristov 2019-04-21 21:36:43 +03:00
parent ad827d19bd
commit bd949bca74

View file

@ -6,7 +6,16 @@ const styles = require('./styles');
const NavBarButton = ({ className, icon, label, href, onClick }) => {
const type = typeof onClick === 'function' ? 'button' : 'link';
const active = window.location.hash === href;
const [active, setActive] = React.useState(window.location.hash === href);
const onHashChanged = React.useCallback(() => {
setActive(window.location.hash === href);
}, [href]);
React.useEffect(() => {
window.addEventListener('hashchange', onHashChanged);
return () => {
window.removeEventListener('hashchange', onHashChanged);
};
}, []);
return (
<Input className={classnames(className, styles['button'], { 'active': active })} type={type} href={href} onClick={onClick}>
<Icon className={styles['icon']} icon={icon} />