react-native 阻止路由返回监听

2023-10-27  本文已影响0人  Biao_349d
    const goBack = (onBack?: () => void) => {
   
        const isSave = true
        const onBackFn = onBack ? onBack : navigation.goBack
        if (!isSave) {
          onBackFn()

          return
        }
        Modal.alert('', <Text>当前内容已修改,是否存为草稿?</Text>, [
          {
            text: '否',
            onPress: () => {
              onBackFn()
            }
          },
          {
            text: '是',
            onPress: () => {
              onSubmitDraft(onBackFn)
            }
          }
        ])
      })
    }

    const navigationBeforeRemove = () => {
      const l = navigation.addListener('beforeRemove', e => {
        console.log(11)
        // Prevent default behavior of leaving the screen
        e.preventDefault()
        // Prompt the user before leaving the screen
        goBack(() => {
          navigation.dispatch(e.data.action)
        })
      })
    }
上一篇 下一篇

猜你喜欢

热点阅读