Ben Young
Ben Young

Reputation: 1

Why do I keep getting a TypeError: setScrollNav is not a function, when function is defined

I am trying to use React Scroll, but with this code in place whenever I scroll I get error:

TypeError: setScrollNav is not a function
export default function Navbar() {
    const [showNav, setShowNav] = useState(false);

    const setScrollNav = useState(false);

    useEffect(() => {
        function changeNav() {
            if(window.scrollY >= 100) {
                setScrollNav(true)
            } else {
                setScrollNav(false)
            }
        }
        window.addEventListener('scroll', changeNav)
        return () => window.removeEventListener('scroll', changeNav)
}, [setScrollNav])
return (

Upvotes: 0

Views: 289

Answers (1)

windmaomao
windmaomao

Reputation: 7670

Here's the syntax of useState

  const [count, setCount] = useState(0)

It returns an array, the second element of the array is the dispatch function.

Upvotes: 1

Related Questions