I have a React Native app in which I am running some code that looks like the code below. Why is the following output undefined
? Reading someState
from useEffect
is undefined
even when the state is set inside the useEffect
const SomeComponent = () => {
const [someState,setSomeState] = useState();
const anFunction = () => {
let data = 'data'
return data
}
useEffect(() => {
const theData = anFunction();
setSomeState(theData)
console.log(someState)
},[])
...
// later renders some component
}
I am attempting set the state and then read the state within this useEffect
because I would like to implement something like:
const doSomethingWithTheState = () => {
//some transformation here
setSomeState(transformedData)
}
useEffect(() => {
const theData = anFunction();
setSomeState(theData)
doSomethingWithTheState()
},[])