【taro react hooks 错误解决】---- Warning: Can‘t perform a React state update on an unmounted component.

1. 错误

1.1 错误内容

Warning: Can't perform a React state update on an unmounted component. 
This is a no-op, but it indicates a memory leak in your application. 
To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

1.2 错误截图

输入图片说明

2. 错误分析

无法对已卸载的组件执行react状态更新。

3. 错误出现的场景

  1. 页面存在异步加载数据等类似操作;
  2. 但是异步加载未完成,就跳转到其他页面,当前页面被卸载;
  3. 卸载后的组件进行状态更新,所以报错。

4. 解决办法 react hooks

  1. useUnmounted 进行判断组件是否被卸载;
  2. useAsyncState 防止在已卸载的组件上更新状态。
import { useCallback, useEffect, useRef, useState } from 'react';

export function

猜你喜欢

转载自blog.csdn.net/m0_38082783/article/details/129694850