Skip to content

Commit 8dfeddb

Browse files
committed
Document setOptions
1 parent 34f38ea commit 8dfeddb

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

docs/navigation-prop.md

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ navigation.navigate({ key: SCREEN_KEY_A }); // will go to screen A FROM screen D
105105

106106
Alternatively, as _screen A_ is the top of the stack, you can use `navigation.popToTop()`.
107107

108-
### Replace
108+
### `replace`
109109

110110
Call this to replace the current screen with the given route, with params and sub-action.
111111

112112
```js
113113
navigation.replace(name, params);
114114
```
115115

116-
### Reset
116+
### `reset`
117117

118118
Replace the navigator state to a new state:
119119

@@ -124,6 +124,41 @@ navigation.reset({
124124
});
125125
```
126126

127+
### `setParams` - Make changes to route params
128+
129+
Firing the `setParams` action allows a screen to change the params in the route, which is useful for updating the header buttons and title. `setParams` works like React's `setState` - it merges the provided params object with the current params.
130+
131+
```js
132+
function ProfileScreen({ navigation: { setParams } }) {
133+
render() {
134+
return (
135+
<Button
136+
onPress={() => setParams({ name: "Lucy" })}
137+
title="Set title name to 'Lucy'"
138+
/>
139+
);
140+
}
141+
}
142+
```
143+
144+
### `setOptions` - Update screen options from the component
145+
146+
The `setOptions` method lets us set screen options from within the component. This is useful if we need to use the component's props, state or context to configure our screen.
147+
148+
```js
149+
function SelectionScreen({ navigation }) {
150+
const [selectCount, setSelectCount] = React.useState(0);
151+
152+
navigation.setOptions({
153+
title: selectCount > 0 : `${selectCount} items selected` : 'Select some items',
154+
});
155+
156+
return <SelectionList onSelectCountChange={setSelectCount} />;
157+
}
158+
```
159+
160+
Any options specified here are shallow merged with the options specified when defining the screen.
161+
127162
## Navigation events
128163

129164
Screens can add listeners on the `navigation` prop like in React Navigation. By default, `focus` and `blur` events are fired when focused screen changes:
@@ -156,23 +191,6 @@ const isFocused = navigation.isFocused();
156191

157192
This method doesn't re-render the screen when the value changes and mainly useful in callbacks. You probably want to use [useIsFocused](use-is-focused.md) instead of using this directly, it will return a boolean a prop to indicating if the screen is focused.
158193

159-
### `setParams` - Make changes to route params
160-
161-
Firing the `setParams` action allows a screen to change the params in the route, which is useful for updating the header buttons and title. `setParams` works like React's `setState` - it merges the provided params object with the current params.
162-
163-
```js
164-
function ProfileScreen({ navigation: { setParams } }) {
165-
render() {
166-
return (
167-
<Button
168-
onPress={() => setParams({ name: "Lucy" })}
169-
title="Set title name to 'Lucy'"
170-
/>
171-
);
172-
}
173-
}
174-
```
175-
176194
## Advanced API Reference
177195

178196
The `dispatch` function is much less commonly used, but a good escape hatch if you can't do what you need with `navigate` and `goBack`.

0 commit comments

Comments
 (0)