save-cloud icon indicating copy to clipboard operation
save-cloud copied to clipboard

Investigate the reason why useRequest is not working with avatarEditor

Open orchestr7 opened this issue 2 years ago • 2 comments

          @sanyavertolet can you please investigate it if you will have time?

Originally posted by @akuleshov7 in https://github.com/saveourtool/save-cloud/pull/2418#discussion_r1285258326

orchestr7 avatar Aug 06 '23 21:08 orchestr7

You should not use useRequest anywhere but on the top level of functional components. That's why we have useDeferredRequest, which has something like useRequest under the hood. When useDeferredRequest is invoked, a callback to execute a request is returned. This callback can be both used on any level of this FC and passed anywhere you want.

Thus, you might want to use useDeferredRequest on the top level of this FC (smth like val uploadImage = useDeferredRequest { REQUEST }) and then invoke it anywhere you want. Please notice that useDeferredRequest does not receive parameters unlike methods of ClassComponent, so you need to create a state variable and set it before uploadImage invocation.

You might also add

useEffect(stateImage) {
    stateImage?.let {
        uploadImage()
        setStateImage(null)
    }
}

if you want uploadImage to be executed when stateImage is set. Notice that useEffect should also be invoked from the top-level of FC as it is a hook

sanyavertolet avatar Aug 07 '23 06:08 sanyavertolet

I will save it also here: https://github.com/saveourtool/save-cloud/pull/2470#discussion_r1295864138

orchestr7 avatar Aug 16 '23 22:08 orchestr7