4
4
46kvolamak2021-08-03 13:27:17
React
46kvolamak, 2021-08-03 13:27:17

How to wait for a hook to execute and then execute another synchronized function?

I have a hook that updates the state of a pdf document

const [instance, update] = usePDF({ document: ShowPdf() })

I need to download the document only after the update happens, but since the hooks are asynchronous, I download the previous version of the document.
I know that you can use useEffect like this:
but then it will be loaded even after rendering, and I need it only when the button is pressed

useEffect(() => {
    downloadPdf()
  }, [instance])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleks_Ko, 2021-08-03
@Aleks_Ko

Maybe add a banal if check?
useEffect(() => {
if(instance) {
downloadPdf()
}
}, [instance])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question