Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsubscribe() to preventing-going-back example #1269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 25 additions & 23 deletions static/examples/5.x/prevent-going-back.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ const EditTextScreen = ({ navigation }) => {

const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}

e.preventDefault();
e.preventDefault();

Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
}),
[hasUnsavedChanges, navigation]
);
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
});

return () => {
unsubscribe();
};
}, [hasUnsavedChanges, navigation]);

return (
<View style={styles.content}>
Expand Down
48 changes: 25 additions & 23 deletions static/examples/6.x/prevent-going-back.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ const EditTextScreen = ({ navigation }) => {

const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}

e.preventDefault();
e.preventDefault();

Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
}),
[hasUnsavedChanges, navigation]
);
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
});

return () => {
unsubscribe();
};
}, [hasUnsavedChanges, navigation]);

return (
<View style={styles.content}>
Expand Down
48 changes: 25 additions & 23 deletions static/examples/7.x/prevent-going-back.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ const EditTextScreen = ({ navigation }) => {

const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
const action = e.data.action;
if (!hasUnsavedChanges) {
return;
}

e.preventDefault();
e.preventDefault();

Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
}),
[hasUnsavedChanges, navigation]
);
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
});

return () => {
unsubscribe();
};
}, [hasUnsavedChanges, navigation]);

return (
<View style={styles.content}>
Expand Down
61 changes: 32 additions & 29 deletions versioned_docs/version-5.x/preventing-going-back.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,38 @@ function EditText({ navigation }) {
const [text, setText] = React.useState('');
const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
}),
[navigation, hasUnsavedChanges]
);
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
});

// Unsubscribe the listener when the component unmounts to avoid memory leaks
return () => {
unsubscribe();
};
}, [navigation, hasUnsavedChanges]);

return (
<TextInput
Expand Down
61 changes: 32 additions & 29 deletions versioned_docs/version-6.x/preventing-going-back.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,38 @@ function EditText({ navigation }) {
const [text, setText] = React.useState('');
const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
}),
[navigation, hasUnsavedChanges]
);
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
});

// Unsubscribe the listener when the component unmounts to avoid memory leaks
return () => {
unsubscribe();
};
}, [navigation, hasUnsavedChanges]);

return (
<TextInput
Expand Down
61 changes: 32 additions & 29 deletions versioned_docs/version-7.x/preventing-going-back.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,38 @@ function EditText({ navigation }) {
const [text, setText] = React.useState('');
const hasUnsavedChanges = Boolean(text);

React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
}),
[navigation, hasUnsavedChanges]
);
React.useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
if (!hasUnsavedChanges) {
// If we don't have unsaved changes, then we don't need to do anything
return;
}

// Prevent default behavior of leaving the screen
e.preventDefault();

// Prompt the user before leaving the screen
Alert.alert(
'Discard changes?',
'You have unsaved changes. Are you sure to discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => navigation.dispatch(e.data.action),
},
]
);
});

// Unsubscribe the listener when the component unmounts to avoid memory leaks
return () => {
unsubscribe();
};
}, [navigation, hasUnsavedChanges]);

return (
<TextInput
Expand Down