|
| 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 visits 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 a higher order component |
| 16 | + |
| 17 | +react-navigation provides a [higher order component](https://reactjs.org/docs/higher-order-components.html) that passes a `isFocused` to our component, along with the `navigation` object we'd normally get with `withNavigation`. |
| 18 | + |
| 19 | +When the prop `isFocused` 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 functions on a user entering or leaving a screen. |
| 20 | + |
| 21 | +### Example |
| 22 | + |
| 23 | +```js |
| 24 | +import React, { Component } from "react"; |
| 25 | +import { View } from "react-native"; |
| 26 | +import { withNavigationFocus } from "react-navigation"; |
| 27 | + |
| 28 | +class TabScreen extends Component { |
| 29 | + componentDidUpdate(prevProps) { |
| 30 | + if (prevProps.isFocused !== this.props.isFocused) { |
| 31 | + // Use the `this.props.isFocused` boolean |
| 32 | + // Call any action |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + render() { |
| 37 | + return <View />; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// withNavigationFocus returns a component that wraps TabScreen and passes |
| 42 | +// in the navigation prop |
| 43 | +export default withNavigationFocus(TabScreen); |
| 44 | +``` |
| 45 | + |
| 46 | +This example is also documented in the `withNavigationFocus` API documentation. |
| 47 | + |
| 48 | +## Triggering an action with an event listener |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +With this approach, we will only be able to call an action when the screen focuses. |
| 53 | + |
| 54 | +### Example |
| 55 | + |
| 56 | +```js |
| 57 | +import React, { Component } from "react"; |
| 58 | +import { View } from "react-native"; |
| 59 | +import { withNavigation } from "react-navigation"; |
| 60 | + |
| 61 | +class TabScreen extends Component { |
| 62 | + componentDidMount() { |
| 63 | + const { navigation } = this.props; |
| 64 | + this.focusListener = navigation.addListener("didFocus", () => { |
| 65 | + // The screen is focused |
| 66 | + // Call any action |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + componentWillUnmount() { |
| 71 | + // Remove the event listener |
| 72 | + this.focusListener.remove(); |
| 73 | + } |
| 74 | + |
| 75 | + render() { |
| 76 | + return <View />; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export default withNavigation(TabScreen); |
| 81 | +``` |
0 commit comments