无法在已卸载的组件上执行React状态更新 =>每当我更改页面时,React Native的useEffect循环都会调用更多次吗?

如何解决无法在已卸载的组件上执行React状态更新 =>每当我更改页面时,React Native的useEffect循环都会调用更多次吗?

堆栈为:

  • TypeScript
  • 反应原生(无人参与)
  • 反应本地Firebase程序包
  • Firebase
  • Firestore

我有一个应用程序,其中创建了2个钩子和一个用于上/下投票的组件。

第一个自定义钩子是通用的Firebase文档处理程序。 每次我更改页面或重新加载页面时,都会越来越多地调用此循环。

我有这个警告:

无法在已卸载的组件上执行React状态更新。这是无操作,但表示您的应用程序中发生内存泄漏。要修复,请取消使用useEffect清理功能中的所有订阅和异步任务。

const useDocument = (ref: DocumentReference,create = false): UseDocumentType => {
  const [documentData,setDocumentData] = useState<DocumentData>({})
  const [documentExists,setDocumentExists] = useState<'untested' | boolean>('untested')
  const [updating,setUpdating] = useState(false)

  // Listen to the user post data
  useEffect(() => {
    // Declare the function that will be used to subscribe
    const subscriber = (): void => {
      try {
        ref?.onSnapshot((documentSnapshot: DocumentSnapshot) => {
          if (documentSnapshot && documentSnapshot.exists) {
            setUpdating(false)
            setDocumentExists(true)
            const data = documentSnapshot.data() || {}
            data.id = documentSnapshot.id
            setDocumentData(data)
            // The screen is covered by the above console.log,while not infinite
            console.log(`DEBUG > useDocument > subscriber > document ${ref.path} data fetched`)
          } else if (create) {
            // Create the document with a single field
            updateDocument({ createdOn: new Date() })
            console.log(`DEBUG > useDocument > subscriber > document ${ref.path} created by subscriber`)
          } else {
            setDocumentExists(false)
            console.log(`WARN > useDocument > subscriber > the document ${ref.path || '<unknown ref>'} does not exists in Firebase`)
          }
        })
      } catch (error) {
        throw functionError(error,'subscriber','','useDocument')
      }
    }

    subscriber()
    // Stop listening for updates when no longer required
    return (): void => subscriber()

    // Disabling ESLint rule since passing refs in hooks dependencies create infinite rerenders
    // Refs are functions and shall be wrapped into useCallback to avoid that.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  },[create,shallExist])

  // Function used to update documents
  const updateDocument = async (data: Partial<DocumentData>): Promise<void> => {
    try {
      setUpdating(true)
      await ref?.set(data,{ merge: true })
      // This function can also create the document
      setDocumentExists(true)
    } catch (error) {
      throw functionError(error,'updateDocument',data,'useDocument')
    }
  }

  return { documentExists,documentData,updating,setUpdating,updateDocument }
}

然后由useFeedback自定义钩子调用:

const useFeedback = (user: AuthUser,postId: string): UseFeedbackType => {
  // State
  const [upvoted,setUpvoted] = useState<boolean>(false)
  const [downvoted,setDownvoted] = useState<boolean>(false)
  // Reference to the document handling user feedback for this post
  // Shall it be passed into a useCallback hook?
  const ref: DocumentReference<DocumentData> = userPostDoc(user,postId)
  // useDocument provides tooling necessary to work with the document
  const { documentExists,updateDocument,toggleDocumentField,withTransaction } = useDocument(ref)

  // Update the state when the document data change
  useEffect(() => {
    console.log(`DEBUG > useFeedback > useEffect > liked=${documentData?.upvoted},muted=${documentData?.downvoted}`)
    if (user) {
      setUpvoted(documentData?.upvoted || false)
      setMuted(documentData?.downvoted|| false)
    }
  },[documentData,user])

  const onUpvote = async (): Promise<void> => {
    if (upvoted) {
      await updateDocument({ upvoted: false })
      setUpvoted(false)
    } else {
      await updateDocument({ upvoted: true})
      setUpvoted(true)
    }
  }

  const onMute = async (): Promise<void> => {
    if (muted) {
      await updateDocument({ downvoted: false })
      setMuted(false)
    } else {
      await updateDocument({ downvoted: true })
      setMuted(true)
    }
  }

  return { onUpvote,onMute,upvoted,muted,updating }
}

然后将该组件调用到屏幕中:

const PostScreen: FC<Props> = ({ route,navigation }: Props) => {
  const initialPostData = route?.params?.post
  //imported: const postDoc = (postId: string) => postsCollection.doc(postId)
  const ref = postDoc(initialPostData.id)
  const [postData,setPostData] = useState(initialPostData)
  const { documentData,documentExists } = useDocument(ref)

  // Force re-render
  useEffect(() => {
    if (documentExists === true) setPostData(documentData)
    // This console.log is also printed several time but "only" ~10 times
    console.log(
      `DEBUG > PostScreen > useEffect > documentExists=${documentExists} and postData=${objectToString(postData || initialPostData)}`,)
  },documentExists])

  return (
    <SafeAreaView style={styles.container}>
      <StatusBar backgroundColor="transparent" barStyle="dark-content" translucent={true} />
      <ScrollView>
        <FavoriteIcon data={postData} style={styles.bookmark} color={colors.white} size={50} />
        <View style={styles.contentContainer}>
          <Text style={styles.title}>{postData?.title}</Text>
          <Text style={styles.description}>{postData?.description || ''}</Text>
          <FeedbackIcons postId={postData?.id} />
        </View>
      </ScrollView>
    </SafeAreaView>
  )
}

我是React的新手。这实际上可行,但更新时间很长(尽管Firebase足够聪明,可以在上/下投票时使用它的缓存,但设置起来需要2秒),并且useEffect的日志记录很长,使我认为存在严重的优化问题...

感谢您的耐心和帮助!

编辑

重新启动Android模拟器后,每次我上/下一篇文章时,只有1个调用useFeedback和3个useDocument。

保存了几次代码后,我现在有10个对useFeedback的调用,每个之间有1到4个对useDocument的调用...并且每次我保存代码或更改页面时,它都会变得越来越糟。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-