简体   繁体   English

React 多次安装和卸载相同的组件

[英]React Same component mounted and unmounted multiple times

When i switch between two tabs in my react app useEffect calls api everytime.当我在我的反应应用程序中的两个选项卡之间切换时,useEffect 每次都会调用 api。

Is it fine to call api on every time on the page mount or only in the first time page mount.每次在页面装载或仅在第一次页面装载时调用 api 是否可以。

If only in first time page mount if we call api,then how to handle data freshness in that page in second mount?如果我们调用 api 仅在第一次页面挂载中,那么如何在第二次挂载时处理该页面中的数据新鲜度?

It is up to your needs when to call api call and how important to keep the freshness of data.何时调用 api 以及保持数据新鲜度的重要性取决于您的需求。 you can either call api call on switch tab or you go one level up and set this to their parents where switch tabs is the children.您可以在开关选项卡上调用 api 调用,或者将 go 向上一级并将其设置为他们的父母,其中开关选项卡是孩子。 you can use many tools like react query, redux, custom caching and etc to handle your needs.您可以使用许多工具,如反应查询、redux、自定义缓存等来满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM