Repository navigation
Migrate useBeforeRemove to usePreventRemove in DiscardChangesConfirmation and fix regressions
#84605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate useBeforeRemove to usePreventRemove in DiscardChangesConfirmation and fix regressions
#84605
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| type UseDiscardChangesConfirmationOptions = { | ||
| getHasUnsavedChanges: () => boolean; | ||
| onCancel?: () => void; | ||
| onVisibilityChange?: (visible: boolean) => void; | ||
| isEnabled?: boolean; | ||
| hasUnsavedChanges: boolean; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ CONSISTENCY-4 (docs)
Remove Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| onVisibilityChange?: (visible: boolean) => void; | ||
| onCancel?: () => void; | ||
| shouldNavigateAfterSave?: boolean; | ||
| navigateBack?: () => void; | ||
| }; | ||
|
|
||
| export default UseDiscardChangesConfirmationOptions; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import {usePreventRemove} from '@react-navigation/native'; | ||
| import type {NavigationAction} from '@react-navigation/native'; | ||
| import React, {memo, useCallback, useRef, useState} from 'react'; | ||
| import ConfirmModal from '@components/ConfirmModal'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import navigationRef from '@libs/Navigation/navigationRef'; | ||
| import type DiscardChangesConfirmationProps from './types'; | ||
|
|
||
| function DiscardChangesConfirmation({hasUnsavedChanges, onVisibilityChange}: DiscardChangesConfirmationProps) { | ||
| const {translate} = useLocalize(); | ||
| const [isVisible, setIsVisible] = useState(false); | ||
| const blockedNavigationAction = useRef<NavigationAction | undefined>(undefined); | ||
|
|
||
| usePreventRemove( | ||
| hasUnsavedChanges, | ||
| useCallback((e) => { | ||
| blockedNavigationAction.current = e.data.action; | ||
| setIsVisible(true); | ||
| }, []), | ||
| ); | ||
|
|
||
| const setModalVisible = useCallback( | ||
| (nextVisible: boolean) => { | ||
| setIsVisible(nextVisible); | ||
| onVisibilityChange?.(nextVisible); | ||
| }, | ||
| [onVisibilityChange], | ||
| ); | ||
|
|
||
| return ( | ||
| <ConfirmModal | ||
| isVisible={isVisible} | ||
| title={translate('discardChangesConfirmation.title')} | ||
| prompt={translate('discardChangesConfirmation.body')} | ||
| danger | ||
| confirmText={translate('discardChangesConfirmation.confirmText')} | ||
| cancelText={translate('common.cancel')} | ||
| onConfirm={() => { | ||
| setIsVisible(false); | ||
| setModalVisible(false); | ||
| if (blockedNavigationAction.current) { | ||
| navigationRef.current?.dispatch(blockedNavigationAction.current); | ||
| blockedNavigationAction.current = undefined; | ||
| } else { | ||
| navigationRef.current?.goBack(); | ||
| } | ||
| }} | ||
| onCancel={() => { | ||
| setModalVisible(false); | ||
| blockedNavigationAction.current = undefined; | ||
| }} | ||
| shouldHandleNavigationBack | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default memo(DiscardChangesConfirmation); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| type DiscardChangesConfirmationProps = { | ||
| onCancel?: () => void; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ CONSISTENCY-4 (docs)
Remove the unused fields or add the consuming code first. Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| hasUnsavedChanges: boolean; | ||
| shouldNavigateAfterSave?: boolean; | ||
| navigateBack?: () => void; | ||
| /** Notifies when the discard modal visibility changes */ | ||
| onVisibilityChange?: (isVisible: boolean) => void; | ||
| }; | ||
|
|
||
| export default DiscardChangesConfirmationProps; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onModalHidebranches onisConfirmed.current, but this ref is never updated totrueanywhere in the hook, so the cancel path runs on every close. In practice this meansonCancelis triggered even after a confirm, and on a real cancel it can run twice (once inonModalHide, once in the.thencancel branch), which causes incorrect side effects for callers that useonCancel(e.g., refocusing inputs while navigating away).Useful? React with 👍 / 👎.