Skip to content

Commit a28b3e9

Browse files
committed
Use popTo for passing params back
1 parent 5f94cb9 commit a28b3e9

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

versioned_docs/version-7.x/params.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ Avoid using `setParams` to update screen options such as `title` etc. If you nee
196196

197197
## Passing params to a previous screen
198198

199-
Params aren't only useful for passing some data to a new screen, but they can also be useful to pass data to a previous screen too. For example, let's say you have a screen with a create post button, and the create post button opens a new screen to create a post. After creating the post, you want to pass the data for the post back to previous screen.
199+
Params aren't only useful for passing some data to a new screen, but they can also be useful to pass data to a previous screen as well. For example, let's say you have a screen with a "Create post" button, and the button opens a new screen to create a post. After creating the post, you want to pass the data for the post back to the previous screen.
200200

201-
To achieve this, you can use the `navigate` method, which acts like `goBack` if the screen already exists. You can pass the `params` with `navigate` to pass the data back:
201+
To achieve this, you can use the `popTo` method to go back to the previous screen as well as pass params to it:
202202

203203
```js name="Passing params back" snack version=7
204204
import * as React from 'react';
@@ -213,13 +213,16 @@ import { createNativeStackNavigator } from '@react-navigation/native-stack';
213213
function HomeScreen({ route }) {
214214
const navigation = useNavigation();
215215

216+
// Use an effect to monitor the update to params
217+
// highlight-start
216218
React.useEffect(() => {
217219
if (route.params?.post) {
218220
// Post updated, do something with `route.params.post`
219221
// For example, send the post to the server
220-
console.log('Post', route.params?.post);
222+
alert('New post: ' + route.params?.post);
221223
}
222224
}, [route.params?.post]);
225+
// highlight-end
223226

224227
return (
225228
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
@@ -248,12 +251,9 @@ function CreatePostScreen({ route }) {
248251
<Button
249252
title="Done"
250253
onPress={() => {
251-
// Pass and merge params back to home screen
252-
navigation.navigate({
253-
name: 'Home',
254-
params: { post: postText },
255-
merge: true,
256-
});
254+
// Pass params back to home screen
255+
// highlight-next-line
256+
navigation.popTo('Home', { post: postText });
257257
}}
258258
/>
259259
</>

0 commit comments

Comments
 (0)