Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

add useAsync custom hook #76

Open
TianyiLi opened this issue Aug 12, 2020 · 3 comments
Open

add useAsync custom hook #76

TianyiLi opened this issue Aug 12, 2020 · 3 comments

Comments

@TianyiLi
Copy link
Contributor

先前的service + setState的方式,有可能造成memory leak

const result = await Axios.get('<url>')
setState(result)

// unmount but during query

image

更新的方式爲採用useRef先對相關的dipatch function / state / request method做ref,然後等unmount清對應的function

@rt-cff
Copy link
Contributor

rt-cff commented Aug 12, 2020

you mentioned that this happened during page navigation, right?
What triggered the page navigation?

I encountered this once, when I tried to do the following:
useEffect(() => { //....Data Fetching Call }, [account_id]);
This way, when user switches account, which it also redirects user to HOME, the above Data Fetching call will be done before navigating to HOME.

If this is the case, other than using another hook to avoid this error, I would suggest removing the dependency instead.

@TianyiLi
Copy link
Contributor Author

use effect + data fetching call, might have an error when applying data.
And this way is more likely to use event bus to trigger the update or send request, When trace or review the code, event bus is not a good option.

@rt-cff
Copy link
Contributor

rt-cff commented Aug 12, 2020

I am not sure, by using another hook, would prevent any memory leak.
But I supposed what you are trying to achieve is to avoid calling any setState when the component is actually being unmounted.
I think there are simpler solution, proposed by Dan, facebook/react#14369 (comment).
You can also refer to this: https://jsfiddle.net/raymond_tam/1o69d3x5/3/
Let me know if there are some other use cases that I missed.

rt-cff added a commit that referenced this issue Apr 19, 2021
feat(module): [conversations][FilterSideBar] Add  filter for created_…

Close #76
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants