tl;dr Scroll down for the solution that worked for me!
How to implement a slide up and down on a fixed navbar in react?
What is the better approach using refs or using the componentDidMount lifecycle hook?
hideNav = (navbar) => {
const hide = () => {
let lastScrollTop = 0;
const currentScrollTop = navbar.scrollTop;
// scroll down
if (currentScrollTop > lastScrollTop) {
navbar.classList.add('hidden');
} else {
// scroll up
navbar.classList.remove('hidden');
}
lastScrollTop = currentScrollTop;
};
window.addEventListener('scroll', hide);
};
... further down in the render method:
render() {
return <Navbar ref={this.hideNav} />
UPDATE:
Solution:
class Navbar extends React.Component {
state = {
auth: false,
slide: 0, // How much should the Navbar slide up or down
lastScrollY: 0, // Keep track of current position in state
};
componentWillMount() {
// When this component mounts, begin listening for scroll changes
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
// If this component is unmounted, stop listening
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = () => {
const { lastScrollY } = this.state;
const currentScrollY = window.scrollY;
if (currentScrollY > lastScrollY) {
this.setState({ slide: '-48px' });
} else {
this.setState({ slide: '0px' });
}
this.setState({ lastScrollY: currentScrollY });
};
render() {
return (
<Navbar
style={{
transform: `translate(0, ${this.state.slide})`,
transition: 'transform 90ms linear',
}}
/>
);
}
}
I haven't done any optimizations yet so it is recommended to throttle the event using requestAnimationFrame, setTimeout or customEvent. Like here.
You shouldn't use refs as a solution to register event listeners or adding/removing classes. As you suggested, you should use component lifecycle hooks to begin (and stop) listening for scrolls on the window.
export default class App extends Component {
state = { hidden: false };
constructor(props) {
super(props);
// Bind the function to this component, so it has access to this.state
this.handleScroll = this.handleScroll.bind(this);
}
componentWillMount() {
// When this component mounts, begin listening for scroll changes
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
// If this component is unmounted, stop listening
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll(e) {
let lastScrollTop = 0;
const currentScrollTop = navbar.scrollTop;
// Set the state of hidden depending on scroll position
// We only change the state if it needs to be changed
if (!this.state.hidden && currentScrollTop > lastScrollTop) {
this.setState({ hidden: true });
} else if(this.state.hidden) {
this.setState({ hidden: false });
}
lastScrollTop = currentScrollTop;
}
render() {
// We pass a hidden prop to Navbar which can render className="hidden" if the prop is true
return (
<Navbar hidden={this.state.hidden} />
);
}
}
Also, looking at the scroll function you provided, it won't work, as lastScrollTop
will always be 0. If you're looking for a scroll solution take a look at this answer as it has a similar solution to what your fixed navbar would need (except, hiding instead of being shown) : Sticky Header after scrolling down
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