gpt4 book ai didi

react-native componentWillUnmount 在导航时不起作用

转载 作者:行者123 更新时间:2023-12-04 22:28:00 50 4
gpt4 key购买 nike

我正在做这个简单的步骤,但 unmount 没有打电话,我不知道为什么。请我需要一个解决方案,我需要卸载才能在导航到另一个屏幕时被调用...

class Homemain extends Component {
constructor(props) {
super(props);
}

componentWillMount(){
alert('willMount')
}
componentDidMount(){
alert('didMount')
}
componentWillUnmount(){
alert('unMount')
}
Details = () => {
this.props.navigation.navigate('routedetailsheader')
}

render() {
return(
<View style={styles.container}>
<TouchableOpacity onPress={() => this.Details()} style={{ flex: .45, justifyContent: 'center', alignItems: 'center', marginTop: '10%', marginRight: '10%' }}>
<Image
source={require('./../Asset/Images/child-notification.png')}
style={{ flex: 1, height: height / 100 * 20, width: width / 100 * 20, resizeMode: 'contain' }} />
<Text
style={{ flex: 0.5, justifyContent: 'center', fontSize: width / 100 * 4, fontStyle: 'italic', fontWeight: '400', color: '#000', paddingTop: 10 }}>Details</Text>
</TouchableOpacity>
</View>
);
}
}
export default (Homemain);

这是我的 RouteConfiguration,我将导航到下一个屏幕。有人可以帮我解决这个错误,以便我可以继续下一步
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { addNavigationHelpers, NavigationActions } from 'react-navigation';
import { connect } from 'react-redux';
import { BackHandler } from 'react-native';
import { Stack } from './navigationConfiguration';

const getCurrentScreen = (navigationState) => {
if (!navigationState) {
return null
}
const route = navigationState.routes[navigationState.index]
if (route.routes) {
return getCurrentScreen(route)
}
return route.routeName
}
class StackNavigation extends Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
navigation: PropTypes.shape().isRequired,
};

constructor(props) {
super(props);
BackHandler.addEventListener('hardwareBackPress', this.backAction);
}

//backAction = () => this.navigator.props.navigation.goBack();

backAction = () => {
const { dispatch, navigation } = this.props;
const currentScreen = getCurrentScreen(navigation)

if (currentScreen === 'Homemain') {
return false
}
else
if (currentScreen === 'Login') {
return false
}

dispatch(NavigationActions.back());
return true;
};

render() {
const { dispatch, navigation } = this.props;

return (
<Stack
ref={(ref) => { this.navigator = ref; }}
navigation={
addNavigationHelpers({
dispatch,
state: navigation,
})
}
/>
);
}
}

export default connect(state => ({ navigation: state.stack }))(StackNavigation);

最佳答案

路由到新屏幕不会卸载当前屏幕。

对于您的用例,您无需在 componentWillUnmount 中编写代码,而是在 Details 本身中调用导航后继续编写代码。

如果您在从新屏幕按返回键返回当前屏幕时正在寻找回调。使用 goBack https://github.com/react-navigation/react-navigation/issues/733 所示

关于react-native componentWillUnmount 在导航时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48535492/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com