From 86684a0a294a5970f8b01824c8f7fe5226556e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Mon, 23 Jan 2017 11:58:03 -0800 Subject: [PATCH] docs(AppState.js): update example to ES6 Summary: If this is the way to go, I'll update the rest of the document :) > Explain the **motivation** for making this change. What existing problem does the pull request solve? - Update basic usage to latest ES6-7 syntax - Provide a working simple example of using AppState Closes https://github.com/facebook/react-native/pull/11879 Differential Revision: D4443891 Pulled By: hramos fbshipit-source-id: 87433e994ee56050e24a3853f24a94b54f5586d4 --- Libraries/AppState/AppState.js | 50 +++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/Libraries/AppState/AppState.js b/Libraries/AppState/AppState.js index c3f5df27f..c4ad345bc 100644 --- a/Libraries/AppState/AppState.js +++ b/Libraries/AppState/AppState.js @@ -44,25 +44,37 @@ const invariant = require('fbjs/lib/invariant'); * while `AppState` retrieves it over the bridge. * * ``` - * getInitialState: function() { - * return { - * currentAppState: AppState.currentState, - * }; - * }, - * componentDidMount: function() { - * AppState.addEventListener('change', this._handleAppStateChange); - * }, - * componentWillUnmount: function() { - * AppState.removeEventListener('change', this._handleAppStateChange); - * }, - * _handleAppStateChange: function(currentAppState) { - * this.setState({ currentAppState, }); - * }, - * render: function() { - * return ( - * Current state is: {this.state.currentAppState} - * ); - * }, + * import React, {Component} from 'react' + * import {AppState, Text} from 'react-native' + * + * class AppStateExample extends Component { + * + * state = { + * appState: AppState.currentState + * } + * + * componentDidMount() { + * AppState.addEventListener('change', this._handleAppStateChange); + * } + * + * componentWillUnmount() { + * AppState.removeEventListener('change', this._handleAppStateChange); + * } + * + * _handleAppStateChange = (nextAppState) => { + * if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') { + * console.log('App has come to the foreground!') + * } + * this.setState({appState: nextAppState}); + * } + * + * render() { + * return ( + * Current state is: {this.state.appState} + * ); + * } + * + * } * ``` * * This example will only ever appear to say "Current state is: active" because