I am new to react and react hooks. I am creating a custom react hook. Why is useState not working as expected.
import { useEffect, useState } from 'react'
export const useMyHook = () => {
const [myVal, setVal] = useState(0) //myVal is 0
useEffect(() => {
initialize()
},[])
const initialize = () => {
setVal(100)
console.log(myVal) // displays 0, not 100 as I expect
}
return myVal
}
// In my component I call it as follows
import { useMyHook } from 'useMyHook'
...
const myHook = useMyHook()