Commit 1550259b authored by XieZhiXiong's avatar XieZhiXiong

fix: 修复组件销毁后还更新state的问题

parent ac2ef148
......@@ -5,7 +5,7 @@
* @LastEditTime: 2021-05-11 10:16:12
* @Description: 页面公用锚点头部
*/
import React, { useState } from 'react';
import React, { useState, useRef, useEffect } from 'react';
import { Anchor } from 'antd';
import { ArrowLeftOutlined } from '@ant-design/icons';
import { history } from 'umi';
......@@ -62,6 +62,16 @@ const AnchorPage = (props: IProps) => {
const [current, setCurrent] = useState(firstKey);
const [rect, measuredRef] = useClientRect();
const mounted = useRef(false);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
const handleBack = () => {
if (onBack) {
onBack();
......@@ -72,11 +82,11 @@ const AnchorPage = (props: IProps) => {
const handleAnchorChange = (currentActiveLink: string) => {
if (currentActiveLink) {
setCurrent('');
mounted.current && setCurrent('');
return;
}
if (!currentActiveLink) {
setCurrent(firstKey);
mounted.current && setCurrent(firstKey);
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment