Reactjs 离开组件导航时如何执行函数?
我想在用户导航到其他屏幕时执行一个函数。。。我尝试在Reactjs 离开组件导航时如何执行函数?,reactjs,react-native,Reactjs,React Native,我想在用户导航到其他屏幕时执行一个函数。。。我尝试在组件willunmount中执行它,但该组件在导航中未卸载。导航 intervalId = 0; componentDidMount() { const me = this; //alert(JSON.stringify(me.props.navigation)); alert(route.name); this.intervalId = setInterval(() => { /
组件willunmount
中执行它,但该组件在导航中未卸载
。导航
intervalId = 0;
componentDidMount() {
const me = this;
//alert(JSON.stringify(me.props.navigation));
alert(route.name);
this.intervalId = setInterval(() => {
//if (this.props.navigation.state.routeName==)
//this.getOrders();
}, 3000);
//alert("llamada");
}
componentWillUnmount() {
//alert("CAMBIANDO");
clearInterval(this.intervalId);
}
当1时,您需要停止计时器。
组件将卸载
,2。组件失去焦点(didBlur
event)
当您的组件再次接收到焦点时(didFocus
事件),您可以重新启用计时器
import { NavigationEvents } from 'react-navigation';
<NavigationEvents
onDidBlur={() => {
clearInterval(this.intervalId);
}}
onDidFocus={() => {
/** Re-enable timer here */
this.intervalId = setInterval(() => {...
}}
/>
从“react navigation”导入{NavigationEvents};
{
clearInterval(此为有效期);
}}
onDidFocus={()=>{
/**在此重新启用计时器*/
this.intervalId=setInterval(()=>{。。。
}}
/>
您想在componentWillUnmount中执行的功能是什么?…您的意思是想在导航到其他组件后取消安装该组件吗?clearInterval(this.intervalId)。我想清除间隔,但当我点导航时,“componentWillUnmount”不执行时,我会告诉您,当我从导航返回时,如何使间隔再次执行?签出编辑。。。