Is it possible to use objects declared in useeffect hook?

I want to use object declared in useEffect hook, like

function App(){
  useEffect(() => {
    let myObject = .....
  }
  
  myObject.myFunction()
}

but IDE gives me an error that myObject is not defined…

I assume that it is not a good way to lead re-rendering objects declared in useEffect hook, but anyway, is there any way to use objects declared in useEffect hook?

>Solution :

I don’t think so, because it’s in different scope.

it would be ok to use state instead, because when useEffect call and object change, you wont get new value in object

like this :

function App(){
  const [myObject,setMyObject] = useState()
  useEffect(() => {
    setMyObject(...)
  },[])

  myObject?.myFunction()
}

Leave a Reply