当我尝试使用React Router时出现此错误,并且在我的reactjs环境中注意到一切正常,但是当我运行npm run build file时,React Router不起作用,
这是一个完整的错误,我发现控制台需要对其进行修复。
index.js: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 the componentWillUnmount method.
in TextTypeEffect (at carousel.js:8)
in div (at carousel.js:7)
in div (at carousel.js:6)
in Carousel (at App.js:71)
in Home (at App.js:41)
答案 0 :(得分:1)
在组件卸载后组件尝试设置状态时会发生这种情况,因此您需要在componentWillUnmount()
中取消订阅状态更新
下面是类组件的示例代码
import React, { Component } from 'react';
class MyComponent extends Component {
_isMounted = false;
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount = () => this._isMounted = false;
setState = (state, callback) => this._isMounted && super.setState(state, callback);
}
答案 1 :(得分:0)
尝试取消订阅您的状态,该组件内部出现错误: 例如
const [didMount, setDidMount] = useState(false);
useEffect(() => {
setDidMount(true);
//You need to unsubscribe before un-mounting component
return () => setDidMount(false);
}, [])