I tried to build a sticky navbar with a drop shadow when scrolled. I tried adding on scroll on the nav, but it doesn't work.
import React, { Component } from 'react'
import { MenuItems } from "./navbar"
import './navbar.css'
import { Button} from "../Button";
class Navbar extends Component {
state = { clicked : false}
scroll = {scrolled : false}
handleScroll = () => {
const offset = window.scrollY;
if (offset > 200) {
this.setState({scrolled : !this.scroll.scrolled})
}
}
handleClick = () => {
this.setState({ clicked: !this.state.clicked})
}
render() {
return(
<div onScroll={this.handleScroll}>
<nav className={this.scroll.scrolled ? "NavbarItems" : "NavbarItems Scroll"}>
<h1 className="navbar-logo">React <i className="fab fa-react"></i></h1>
<div className="menu-icon" onClick={this.handleClick}>
<i className={this.state.clicked ? "fas fa-times" : 'fas fa-bars'}></i>
</div>
<ul className={this.state.clicked ? 'nav-menu active' : 'nav-menu'}>
{
MenuItems.map((items, index) => {
return (
<li key = {index}><a className={items.cName} href={items.url}>
{items.title}
</a></li>
);
})
}
</ul>
<Button> Contact US </Button>
</nav>
</div>
);
}
}
export default Navbar
What did I do wrong? is it not possible to do it like that?? I'm pretty new to react and javascript, so I'm guessing this isn't the right method to do it.
If there's any reference, please let me know. it would be delightful.