O'Reilly logo

React Native By Example by Richard Kho

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding PostView navigator and BackAndroid support

Now, let's modify the PostView component with the following changes:

  • Import the Platform and BackAndroid APIs
  • Add and remove listeners for BackAndroid during componentWillMount and componentWillUnmount
  • Write a callback in the component to handle back button presses on Android, calling pop on the navigator as a result
  • Create conditional logic similar to FeedListRow to push the WebViewComponent

I created an event listener for BackAndroid in the componentWillMount lifecycle:

 // Friends/app/components/PostView/index.js ... import { BackAndroid, Platform, ... } from 'react-native'; ... export default class PostView extends Component { ... async componentWillMount () { BackAndroid.addEventListener('hardwareButtonPress', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required