Skip to content

Commit 4df002d

Browse files
Jacsespencercarli
authored andcommitted
Add 'save-pattern' to docs (react-navigation#2806)
* Add save-pattern to docs, react-navigation#145 * Sync style * Language clarity * Typo
1 parent 2ee8548 commit 4df002d

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

docs/guides/Guide-Headers.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ The navigation options can be defined with a [navigation prop](/docs/navigators/
5858
5959
```js
6060
static navigationOptions = ({ navigation }) => {
61-
const {state, setParams} = navigation;
61+
const { state, setParams } = navigation;
6262
const isInfo = state.params.mode === 'info';
63-
const {user} = state.params;
63+
const { user } = state.params;
6464
return {
6565
title: isInfo ? `${user}'s Contact Info` : `Chat with ${state.params.user}`,
6666
headerRight: (
6767
<Button
6868
title={isInfo ? 'Done' : `${user}'s info`}
69-
onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
69+
onPress={() => setParams({ mode: isInfo ? 'none' : 'info' })}
7070
/>
7171
),
7272
};
@@ -79,4 +79,60 @@ Now, the header can interact with the screen route/state:
7979
header-interaction
8080
```
8181
82+
### Header interaction with screen component
83+
84+
Sometimes it is necessary for the header to access properties of the screen component such as functions or state.
85+
86+
Let's say we want to create an 'edit contact info' screen with a save button in the header. We want the save button to be replaced by an `ActivityIndicator` while saving.
87+
88+
```js
89+
class EditInfoScreen extends React.Component {
90+
static navigationOptions = ({ navigation }) => {
91+
const { params = {} } = navigation.state;
92+
let headerRight = (
93+
<Button
94+
title="Save"
95+
onPress={params.handleSave ? params.handleSave : () => null}
96+
/>
97+
);
98+
if (params.isSaving) {
99+
headerRight = <ActivityIndicator />;
100+
}
101+
return { headerRight };
102+
};
103+
104+
state = {
105+
nickname: 'Lucy jacuzzi'
106+
}
107+
108+
_handleSave = () => {
109+
// Update state, show ActivityIndicator
110+
this.props.navigation.setParams({ isSaving: true });
111+
112+
// Fictional function to save information in a store somewhere
113+
saveInfo().then(() => {
114+
this.props.navigation.setParams({ isSaving: false});
115+
})
116+
}
117+
118+
componentDidMount() {
119+
// We can only set the function after the component has been initialized
120+
this.props.navigation.setParams({ handleSave: this._handleSave });
121+
}
122+
123+
render() {
124+
return (
125+
<TextInput
126+
onChangeText={(nickname) => this.setState({ nickname })}
127+
placeholder={'Nickname'}
128+
value={this.state.nickname}
129+
/>
130+
);
131+
}
132+
}
133+
```
134+
135+
**Note**: Since the `handleSave`-param is only set on component mount it is not immidiately available in the `navigationOptions`-function. Before `handleSave` is set we pass down an empty function to the `Button`-component in order to make it render immidiately and avoid flickering.
136+
137+
82138
To see the rest of the header options, see the [navigation options document](/docs/navigators/navigation-options#Stack-Navigation-Options).

0 commit comments

Comments
 (0)