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
This commit is contained in:
parent
7412340175
commit
86684a0a29
|
@ -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 (
|
||||
* <Text>Current state is: {this.state.currentAppState}</Text>
|
||||
* );
|
||||
* },
|
||||
* 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 (
|
||||
* <Text>Current state is: {this.state.appState}</Text>
|
||||
* );
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* This example will only ever appear to say "Current state is: active" because
|
||||
|
|
Loading…
Reference in New Issue