[英]React - I need to perform async state updates and useEffect even if the component has been unmounted
Note: in this project, we are not using Redux.注意:在这个项目中,我们没有使用 Redux。
I have a stack profile screen with a simple button "Follow user" and a counter "Followers".我有一个堆栈配置文件屏幕,其中包含一个简单的按钮“关注用户”和一个计数器“关注者”。
A user can navigate to this stack profile screen, and close it whenever he wants.用户可以导航到这个堆栈配置文件屏幕,并在需要时随时关闭它。
If the user press the "Follow" button:如果用户按下“关注”按钮:
3.1 The button will perform a connection to my server, in order to run the logic for "following user" 3.1 该按钮将连接到我的服务器,以运行“关注用户”的逻辑
3.2 Then, when this action is done, to re-render the screen (the other user's followers counter), I am updating the other user's data (state of the profile screen), increasing his followers counter. 3.2 然后,当这个动作完成后,重新渲染屏幕(其他用户的关注者计数器),我正在更新其他用户的数据(个人资料屏幕的状态),增加他的关注者计数器。
3.3 When this state is updated, a useEffect will run and add the new other user data to a map that I have in a context (in which I 'store' users in a visitedUsers map, in order to avoid requesting the database every time I visit the profile of a user I have recently visited, and preserve the correct data between multiple profile screens of the same user in different tabs of the app (some kind of non-persistant local memory and state manager)). 3.3 当这个 state 更新时,useEffect 将运行并将新的其他用户数据添加到我在上下文中的 map(我在其中将用户“存储”在 visitedUsers map 中,以避免每次我请求数据库访问我最近访问过的用户的个人资料,并在应用程序的不同选项卡中保留同一用户的多个个人资料屏幕之间的正确数据(某种非持久性本地 memory 和 state 管理器))。
Steps 3.1, 3.2 and 3.3 always have to run , in order to ensure the app data consistency.步骤 3.1、3.2 和 3.3 始终要运行,以确保应用程序数据的一致性。 But... the user can close the screen during the asynchronous job... so, steps 3.2 and 3.3 might not run.但是......用户可以在异步作业期间关闭屏幕......因此,步骤 3.2 和 3.3 可能不会运行。
Here is a diagram which shows the issue:这是显示问题的图表:
As you can see, if the profile screen is unmounted, the setState and useEffect (which updates the context) will not run and might causes memory leaks.如您所见,如果卸载配置文件屏幕,setState 和 useEffect(更新上下文)将不会运行并可能导致 memory 泄漏。
How can I solve this issue??我该如何解决这个问题??
The "isMounted" anti-pattern is not a good option because steps 3.2 and 3.3 always have to run. “isMounted”反模式不是一个好的选择,因为步骤 3.2 和 3.3 总是必须运行。
It seems that I need to move this logic up into the components hierarchy (what I have thought to do, but seems really strange)... is this the only way?看来我需要将这个逻辑向上移动到组件层次结构中(我想做的,但看起来真的很奇怪)......这是唯一的方法吗?
Any ideas?有任何想法吗?
I had a similar problem once.我曾经遇到过类似的问题。 I was trying to start an upload but allow the user navigate away from the component while the upload was running.我试图开始上传,但允许用户在上传运行时离开组件。 My solution was a bit roundabout but, well, it worked.我的解决方案有点迂回,但好吧,它奏效了。
So what I did was move the upload function to the root component (App.js, not index.js,) and instead of using Redux, I used ContextAPI and set a useEffect hook that would listen to changes in the data stored and, each time the data changed.所以我所做的是将上传 function 移动到根组件(App.js,而不是 index.js),而不是使用 Redux,我使用 ContextAPI 并设置一个 useEffect 钩子来监听存储数据的变化,并且,每个数据改变的时间。 it would trigger the upload function using the new data it finds, That way.它将使用它找到的新数据触发上传 function,这样。 the actual upload is not handled by the component it was initiated from and the user is free to navigate elsewhere without interrupting the upload.实际的上传不由发起它的组件处理,用户可以自由导航到其他地方而不会中断上传。 The only way to interrupt it would be to close the app.中断它的唯一方法是关闭应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.