Skip to content

Commit

Permalink
docs(AppState.js): update example to ES6
Browse files Browse the repository at this point in the history
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 facebook#11879

Differential Revision: D4443891

Pulled By: hramos

fbshipit-source-id: 87433e994ee56050e24a3853f24a94b54f5586d4
  • Loading branch information
hramos authored and facebook-github-bot committed Jan 23, 2017
1 parent 7412340 commit 86684a0
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions Libraries/AppState/AppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 86684a0

Please sign in to comment.