save-cloud
save-cloud copied to clipboard
Investigate the reason why useRequest is not working with avatarEditor
@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
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
I will save it also here: https://github.com/saveourtool/save-cloud/pull/2470#discussion_r1295864138