- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直遇到这样的问题:刷新当前页面(以及随后的组件)时,componentDidMount()
方法中的代码无法正确触发。但是,只需通过单击链接在我的网站中导航和路由,它就可以完美地工作。刷新当前页面?没有机会。
我发现问题在于,当我刷新页面并触发精细点击链接并导航我的网站/应用程序时,componentWillUnmount()
不会触发。
componentWillUnmount()
的触发对于我的应用至关重要,因为我在 componentDidMount()
方法中加载和处理的数据对于显示信息非常重要给用户。
我需要在刷新页面时调用 componentWillUnmount()
,因为在我的 componentWillMount()
函数中(每次刷新后都需要重新渲染)我这样做一些简单的过滤并将该变量存储在状态值中,该状态值需要存在于 logos
状态变量中才能使组件的其余部分正常工作。在组件生命周期的任何时候,这都不会更改或接收新值。
componentWillMount(){
if(dataReady.get(true)){
let logos = this.props.questions[0].data.logos.length > 0 ? this.props.questions[0].data.logos.filter((item) => {
if(item.logo === true && item.location !== ""){
return item;
}
}) : [];
this.setState({logos: logos});
}
};
悬崖:
componentWillMount()
方法中进行数据库过滤componentWillUnmount()
不会触发最佳答案
当页面刷新时,React 没有机会像平常一样卸载组件。使用window.onbeforeunload
设置刷新处理程序的事件(阅读代码中的注释):
class Demo extends React.Component {
constructor(props, context) {
super(props, context);
this.componentCleanup = this.componentCleanup.bind(this);
}
componentCleanup() { // this will hold the cleanup code
// whatever you want to do when the component is unmounted or page refreshes
}
componentWillMount(){
if(dataReady.get(true)){
let logos = this.props.questions[0].data.logos.length > 0 ? this.props.questions[0].data.logos.filter((item) => {
if(item.logo === true && item.location !== ""){
return item;
}
}) : [];
this.setState({ logos });
}
}
componentDidMount(){
window.addEventListener('beforeunload', this.componentCleanup);
}
componentWillUnmount() {
this.componentCleanup();
window.removeEventListener('beforeunload', this.componentCleanup); // remove the event handler for normal unmounting
}
}
useWindowUnloadEffect Hook
我已将代码提取到基于 useEffect
的可重用 Hook 中:
// The hook
const { useEffect, useRef, useState } = React
const useWindowUnloadEffect = (handler, callOnCleanup) => {
const cb = useRef()
cb.current = handler
useEffect(() => {
const handler = () => cb.current()
window.addEventListener('beforeunload', handler)
return () => {
if(callOnCleanup) handler()
window.removeEventListener('beforeunload', handler)
}
}, [callOnCleanup])
}
// Usage example
const Child = () => {
useWindowUnloadEffect(() => console.log('unloaded'), true)
return <div>example</div>
}
const Demo = () => {
const [show, changeShow] = useState(true)
return (
<div>
<button onClick={() => changeShow(!show)}>{show ? 'hide' : 'show'}</button>
{show ? <Child /> : null}
</div>
)
}
ReactDOM.render(
<Demo />,
root
)
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
关于reactjs - 刷新当前页面时未调用 componentWillUnmount(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39084924/
我在类组件中有以下代码,我正在尝试测试 componentWillUnmount。 export class Hello extends React.PureComponent { constru
App.js import React from "react"; import CounterOne from "./CounterOne"; function App() { const [d
我正在使用出色的 create-react-app 工具来构建应用程序。我的问题正如标题所暗示的那样微不足道,但我无法解决。我已经在 github 上发布了一个简单的应用程序来说明: https://
我已经构建了一个表单,其中有一个下拉菜单,将显示下面字段的选择。也就是说,首先选择了 optionSet1,其中显示了 3 个字段。如果用户更改下拉列表以选择 optionSet2,则会显示不同的选项
我一直遇到这样的问题:刷新当前页面(以及随后的组件)时,componentDidMount() 方法中的代码无法正确触发。但是,只需通过单击链接在我的网站中导航和路由,它就可以完美地工作。刷新当前页面
我有一个倒计时计时器,一旦它变为 0,它应该调用一个方法。然后,呈现一个新页面,倒计时应该重置并重新开始。它按预期工作,直到组件卸载。然后每秒调用方法 timeNext(),因为间隔不再停止。 imp
我有一个主干应用程序,我试图在其中集成 React 组件。 React 组件使用以下代码安装: ReactDOM.render( , node );其中 node 是 DOM 模式。这样 React
我正在做这个简单的步骤,但 unmount 没有打电话,我不知道为什么。请我需要一个解决方案,我需要卸载才能在导航到另一个屏幕时被调用... class Homemain extends Compon
我试图了解 componentWillUnmount 在 ReactJS 中的工作原理。它是自动调用的还是我们需要手动调用 unmountComponentAtNode 才能使其工作? 我正在使用带有
我正在reactjs-flux上编写一个简单的应用程序,一切正常,除了我收到reactjs的警告,告诉我我正在对未安装的组件调用setState。 我发现这是因为组件所 Hook 的变更监听器没有从
我认为标题已经说明了一切。每次卸载仍在获取的组件时,都会显示黄色警告。安慰 Warning: Can't call setState (or forceUpdate) on an unmounted
我在窗口中添加了一个监听器来检测 onClickOutside 类型的场景(具体来说,当在菜单外单击时折叠菜单)。这是带有相关代码的组件: import React, { Component, Pro
我有一个带有 redux-form 的表单设置,基本上想创建一个场景,如果在任何表单的输入中填充了内容,并且您尝试离开页面,您会收到提示。 目的是在他们单击取消 时取消页面卸载或页面导航。我尝试创建一
有人可以让我知道我在 componentWillUnmount 我的代码中缺少什么吗。 错误是 Warning: Can't perform a React state update on an un
我在 componentDidMount 中的应用程序中执行服务器请求。所以我在 componentDidMount 中调用 setState。我需要卸载吗componentWillUnmount 中
我正在使用静态方法来实现我的监听器功能。示例: class MyComponent extends Component { static heavyCalculation() { console.
根据docs , ComponentWillUnmount 能够取消请求。 我有一个页面请求加载 iframe,以及其他页面请求。如果用户决定在页面仍在处理请求时离开该页面,我该如何取消这些请求,以免
我有一个正在运行的加载组件,它在加载 8 秒后取消。这段代码有效,但对我来说感觉不对,我想知道是否有更好的方法来做到这一点。 没有设置 this.mounted 我得到错误: Warning: Can
我正在制作一个小倒数计时器作为 React 练习(为我自己,而不是类或其他任何东西)并且一切正常(尽管笔记总是受欢迎的)除了我注意到即使在组件完成后它也会继续倒计时卸载。 所以现在我想让它在卸载时停止
我设置如下代码。 componentWillUnmount() { this.setState({'modal':false}) or this.setState({}) } 但状态不清楚。我
我是一名优秀的程序员,十分优秀!