The given custom hook has a variable count and a setCount function and it return count and another function that under the hood uses setCount to change the count variable.
I am curious to know how this works. Is it because the function useCounter creates a closure?
import {useState} from "react"
function useCounter() {
const [count, setCount] = useState(0)
function increment() {
setCount(prevCount => prevCount + 1)
}
return [count, increment]
}
export default useCounter