切换到另一个屏幕时触发导航,在该屏幕上触发功能

时间:2020-08-14 08:56:06

标签: javascript reactjs react-native

当切换到另一个屏幕时,如何在该屏幕上触发功能? 我从LeadScreen屏幕打开AddLeadScreen屏幕。我从AddLeadScreen屏幕添加了一个销售线索,当我说好时,我希望它返回到LeadScreen屏幕,但刷新该页面。我该怎么办?

Alert.alert(
    'Success',
    'Added Lead.',
    [
        { text: 'Okay', onPress: () =>  navigate('Lead') }
    ]
);

1 个答案:

答案 0 :(得分:2)

您可以这样操作:

AddLeadScreen.js

this.props.navigation.navigate('Lead', { refreshPage: true })

LeadScreen.js

componentDidUpdate(prevProps, prevState) {
    if (this.props.navigation.state && this.props.navigation.state.params && prevProps.navigation.state) {
        if (this.props.navigation.state.params !== prevProps.navigation.state.params) {
            const shouldRefresh = this.props.navigation.state.params.refreshPage
            if (shouldRefresh) {
               // actually refresh the page
            }
        }
    }
}