Skip to content

Commit b324302

Browse files
authored
Merge pull request #330 from jonsamp/js-action-on-focus
Adds guide: action on focus
2 parents 6006881 + 50d6871 commit b324302

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

docs/action-after-focusing-screen.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
id: version-3.x-action-after-focusing-screen
3+
title: Call an Action After Focusing Screen
4+
sidebar_label: Action After Focusing Screen
5+
original_id: action-after-focusing-screen
6+
---
7+
8+
In this guide we will call an action on screen focusing. This is useful for making additional API calls when a user revisits a particular screen in a Tab Navigator, or to track user events as they tap around our app.
9+
10+
There are two approaches to calling an action on screen focusing:
11+
12+
1. Using the `withNavigationFocus` higher order component provided by react-navigation.
13+
2. Listening to the `'didFocus'` event with an event listener.
14+
15+
## Triggering an action with the `withNavigationFocus` higher order component
16+
17+
react-navigation provides a [higher order component](https://reactjs.org/docs/higher-order-components.html) that passes an `isFocused` prop to our component, along with the `navigation` object we'd normally get with `withNavigation`.
18+
19+
When the `isFocused` prop is passed to our component, it will pass `true` when the screen is focused and `false` when our component is no longer focused. This enables us to call actions on a user entering or leaving a screen. This is particularly handy when we are trying to stop something when the page is unfocused, like stopping a video or audio file from playing, or stopping the tracking of a user's location.
20+
21+
Since `withNavigationFocus` passes a prop on every focus change, it will cause our component to re-render when we focus and unfocus a screen. Using this higher order component may introduce unnecessary component re-renders as a screen comes in and out of focus. This could cause issues depending on the type of action we're calling on focusing.
22+
23+
For instance, if we are attempting to make an API call on focus to fetch some data, we only want to fetch data when the component is focused and not when the component becomes unfocused. To prevent extra component re-renders, we could write some logic in `shouldComponentUpdate` to control when the component renders itself, however we may be better off using the event listener method detailed below. The event listener will only call an action and render the component when the screen is focused and will do nothing when a screen becomes unfocused.
24+
25+
### Example
26+
27+
```js
28+
import React, { Component } from "react";
29+
import { View } from "react-native";
30+
import { withNavigationFocus } from "react-navigation";
31+
32+
class TabScreen extends Component {
33+
componentDidUpdate(prevProps) {
34+
if (prevProps.isFocused !== this.props.isFocused) {
35+
// Use the `this.props.isFocused` boolean
36+
// Call any action
37+
}
38+
}
39+
40+
render() {
41+
return <View />;
42+
}
43+
}
44+
45+
// withNavigationFocus returns a component that wraps TabScreen and passes
46+
// in the navigation prop
47+
export default withNavigationFocus(TabScreen);
48+
```
49+
50+
This example is also documented in the <a href="/docs/en/with-navigation-focus.html">`withNavigationFocus` API documentation</a>.
51+
52+
## Triggering an action with a `'didFocus'` event listener
53+
54+
We can also listen to the `'didFocus'` event with an event listener. After setting up an event listener, we must also stop listening to the event when the screen is unmounted.
55+
56+
With this approach, we will only be able to call an action when the screen focuses. This is great for fetching data with an API call when a screen becomes focused, or any other action that needs to happen once the screen comes into view.
57+
58+
### Example
59+
60+
```js
61+
import React, { Component } from "react";
62+
import { View } from "react-native";
63+
import { withNavigation } from "react-navigation";
64+
65+
class TabScreen extends Component {
66+
componentDidMount() {
67+
const { navigation } = this.props;
68+
this.focusListener = navigation.addListener("didFocus", () => {
69+
// The screen is focused
70+
// Call any action
71+
});
72+
}
73+
74+
componentWillUnmount() {
75+
// Remove the event listener
76+
this.focusListener.remove();
77+
}
78+
79+
render() {
80+
return <View />;
81+
}
82+
}
83+
84+
export default withNavigation(TabScreen);
85+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
id: version-3.x-action-after-focusing-screen
3+
title: Call an Action After Focusing Screen
4+
sidebar_label: Action After Focusing Screen
5+
original_id: action-after-focusing-screen
6+
---
7+
8+
In this guide we will call an action on screen focusing. This is useful for making additional API calls when a user revisits a particular screen in a Tab Navigator, or to track user events as they tap around our app.
9+
10+
There are two approaches to calling an action on screen focusing:
11+
12+
1. Using the `withNavigationFocus` higher order component provided by react-navigation.
13+
2. Listening to the `'didFocus'` event with an event listener.
14+
15+
## Triggering an action with the `withNavigationFocus` higher order component
16+
17+
react-navigation provides a [higher order component](https://reactjs.org/docs/higher-order-components.html) that passes an `isFocused` prop to our component, along with the `navigation` object we'd normally get with `withNavigation`.
18+
19+
When the `isFocused` prop is passed to our component, it will pass `true` when the screen is focused and `false` when our component is no longer focused. This enables us to call actions on a user entering or leaving a screen. This is particularly handy when we are trying to stop something when the page is unfocused, like stopping a video or audio file from playing, or stopping the tracking of a user's location.
20+
21+
Since `withNavigationFocus` passes a prop on every focus change, it will cause our component to re-render when we focus and unfocus a screen. Using this higher order component may introduce unnecessary component re-renders as a screen comes in and out of focus. This could cause issues depending on the type of action we're calling on focusing.
22+
23+
For instance, if we are attempting to make an API call on focus to fetch some data, we only want to fetch data when the component is focused and not when the component becomes unfocused. To prevent extra component re-renders, we could write some logic in `shouldComponentUpdate` to control when the component renders itself, however we may be better off using the event listener method detailed below. The event listener will only call an action and render the component when the screen is focused and will do nothing when a screen becomes unfocused.
24+
25+
### Example
26+
27+
```js
28+
import React, { Component } from "react";
29+
import { View } from "react-native";
30+
import { withNavigationFocus } from "react-navigation";
31+
32+
class TabScreen extends Component {
33+
componentDidUpdate(prevProps) {
34+
if (prevProps.isFocused !== this.props.isFocused) {
35+
// Use the `this.props.isFocused` boolean
36+
// Call any action
37+
}
38+
}
39+
40+
render() {
41+
return <View />;
42+
}
43+
}
44+
45+
// withNavigationFocus returns a component that wraps TabScreen and passes
46+
// in the navigation prop
47+
export default withNavigationFocus(TabScreen);
48+
```
49+
50+
This example is also documented in the <a href="/docs/en/with-navigation-focus.html">`withNavigationFocus` API documentation</a>.
51+
52+
## Triggering an action with a `'didFocus'` event listener
53+
54+
We can also listen to the `'didFocus'` event with an event listener. After setting up an event listener, we must also stop listening to the event when the screen is unmounted.
55+
56+
With this approach, we will only be able to call an action when the screen focuses. This is great for fetching data with an API call when a screen becomes focused, or any other action that needs to happen once the screen comes into view.
57+
58+
### Example
59+
60+
```js
61+
import React, { Component } from "react";
62+
import { View } from "react-native";
63+
import { withNavigation } from "react-navigation";
64+
65+
class TabScreen extends Component {
66+
componentDidMount() {
67+
const { navigation } = this.props;
68+
this.focusListener = navigation.addListener("didFocus", () => {
69+
// The screen is focused
70+
// Call any action
71+
});
72+
}
73+
74+
componentWillUnmount() {
75+
// Remove the event listener
76+
this.focusListener.remove();
77+
}
78+
79+
render() {
80+
return <View />;
81+
}
82+
}
83+
84+
export default withNavigation(TabScreen);
85+
```

website/versioned_sidebars/version-3.x-sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"version-3.x-screen-tracking",
3131
"version-3.x-state-persistence",
3232
"version-3.x-redux-integration",
33-
"version-3.x-web-support"
33+
"version-3.x-web-support",
34+
"version-3.x-action-after-focusing-screen"
3435
],
3536
"Build your own Navigator": [
3637
"version-3.x-custom-navigator-overview",

0 commit comments

Comments
 (0)