window.addEventListner, functions, IconContext not working in my functional component React.
I have it change to red when scrolling but it isn’t, also, the color I set doesn’t change, and toggleHome doesn’t scroll to the top, nothing happens when I click the logo.
I want it to acknowledge scrolling and scroll to the top when the logo is pressed.
import React, { useState, useEffect } from "react";
import { FaBars } from 'react-icons/fa'
import { IconContext } from 'react-icons/lib';
import { animateScroll as scroll } from 'react-scroll'
import {
Nav,
NavbarContainer,
NavLogo,
MobileIcon,
NavMenu,
NavItem,
NavLinks,
NavBtn,
NavBtnLink
} from "./NavbarElements";
const Navbar = ({ toggle }) => {
const [scrollNav, setScrollNav] = useState(false);
const changeNav = () => {
if(window.scrollY >= 80) {
setScrollNav(true);
} else {
setScrollNav(false);
}
};
useEffect(() => {
this. window.addEventListener('scroll', changeNav)
}, [changeNav]);
const toggleHome = () => {
scroll.scrollToTop();
}
return (
<>
<IconContext.Provider value={{ color: '#fff' }}>
<Nav scrollNav={scrollNav}>
<NavbarContainer>
<NavLogo to="/" onClick={toggleHome}>dolla</NavLogo>
<MobileIcon onClick={toggle} >
<FaBars />
</MobileIcon>
<NavMenu>
<NavItem>
<NavLinks to="about">About</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="discover">Discover</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="services">Services</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="signup">Sign Up</NavLinks>
</NavItem>
</NavMenu>
<NavBtn>
<NavBtnLink to='/signin'>Sign In</NavBtnLink>
</NavBtn>
</NavbarContainer>
</Nav>
</IconContext.Provider>
</>
);
};
export default Navbar;
import styled from 'styled-components'
import { Link as LinkR } from 'react-router-dom'
import { Link as LinkS } from 'react-scroll'
export const Nav = styled.nav`
background: ${({scrollNav}) => (scrollNav ? 'red' : 'transparent')};
height: 80px;
margin-top: -80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
@media screen and (max-width: 960px) {
transition: 0.8s all ease;
}
`;
export const NavbarContainer = styled.div`
display: flex;
justify-content: space-between;
height: 80px;
z-index: 1;
width: 100%;
padding: 0 24px;
max-width: 1100px;
`;
export const NavLogo = styled(LinkR)`
color: #fff;
justify-self: flex-start;
cursor: pointer;
font-size: 1.5rem;
display: flex;
align-items: center;
margin-left: 24px;
font-weight: bold;
text-decoration: none;
`;
export const MobileIcon = styled.div`
display: none;
@media screen and (max-width: 768px){
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
color: #fff
}
`;
export const NavMenu = styled.ul`
display: flex;
align-items: center;
list-style: none;
text-align: center;
margin-right: -22px;
@media screen and (max-width: 768px) {
display: none;
}
`;
export const NavItem = styled.li`
height: 80px;
`;
export const NavLinks = styled(LinkS)`
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 1rem;
height: 100%;
cursor: pointer;
&.active {
border-bottom: 3px solid #01bf71;
}
`;
export const NavBtn = styled.nav`
display: flex;
align-items: center;
@media screen and (max-width: 768px) {
display: none;
}
`;
export const NavBtnLink = styled(LinkR)`
border-radius: 50px;
background: #01bf71;
white-space: nowrap;
padding: 10px 22px;
color: #010606;
font-size: 16px;
outline: none;
border: none;
cursor: pointer;
transition: all 0.2s ease-in-out;
text-decoration: none;
&:hover {
transition: all 0.2s ease-in-out;
background: #fff;
color: #010606;
}
` ;