Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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()
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading