mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-12 11:40:33 +08:00
Migrate additional docs to the new format
Summary: [DOCS] Closes https://github.com/facebook/react-native/pull/16874 Differential Revision: D6375515 Pulled By: hramos fbshipit-source-id: 64359b45a37c7b478919121573ca04dbb1ce6609
This commit is contained in:
committed by
Facebook Github Bot
parent
f587f8d51d
commit
79e24ede40
@@ -23,66 +23,8 @@ const invariant = require('fbjs/lib/invariant');
|
||||
* `AppState` can tell you if the app is in the foreground or background,
|
||||
* and notify you when the state changes.
|
||||
*
|
||||
* AppState is frequently used to determine the intent and proper behavior when
|
||||
* handling push notifications.
|
||||
*
|
||||
* ### App States
|
||||
*
|
||||
* - `active` - The app is running in the foreground
|
||||
* - `background` - The app is running in the background. The user is either
|
||||
* in another app or on the home screen
|
||||
* - `inactive` - This is a state that occurs when transitioning between
|
||||
* foreground & background, and during periods of inactivity such as
|
||||
* entering the Multitasking view or in the event of an incoming call
|
||||
*
|
||||
* For more information, see
|
||||
* [Apple's documentation](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html)
|
||||
*
|
||||
* ### Basic Usage
|
||||
*
|
||||
* To see the current state, you can check `AppState.currentState`, which
|
||||
* will be kept up-to-date. However, `currentState` will be null at launch
|
||||
* while `AppState` retrieves it over the bridge.
|
||||
*
|
||||
* ```
|
||||
* 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
|
||||
* the app is only visible to the user when in the `active` state, and the null
|
||||
* state will happen only momentarily.
|
||||
* See http://facebook.github.io/react-native/docs/appstate.html
|
||||
*/
|
||||
|
||||
class AppState extends NativeEventEmitter {
|
||||
|
||||
_eventHandlers: Object;
|
||||
@@ -104,10 +46,10 @@ class AppState extends NativeEventEmitter {
|
||||
|
||||
let eventUpdated = false;
|
||||
|
||||
// TODO: this is a terrible solution - in order to ensure `currentState` prop
|
||||
// is up to date, we have to register an observer that updates it whenever
|
||||
// the state changes, even if nobody cares. We should just deprecate the
|
||||
// `currentState` property and get rid of this.
|
||||
// TODO: this is a terrible solution - in order to ensure `currentState`
|
||||
// prop is up to date, we have to register an observer that updates it
|
||||
// whenever the state changes, even if nobody cares. We should just
|
||||
// deprecate the `currentState` property and get rid of this.
|
||||
this.addListener(
|
||||
'appStateDidChange',
|
||||
(appStateData) => {
|
||||
@@ -117,8 +59,8 @@ class AppState extends NativeEventEmitter {
|
||||
);
|
||||
|
||||
// TODO: see above - this request just populates the value of `currentState`
|
||||
// when the module is first initialized. Would be better to get rid of the prop
|
||||
// and expose `getCurrentAppState` method directly.
|
||||
// when the module is first initialized. Would be better to get rid of the
|
||||
// prop and expose `getCurrentAppState` method directly.
|
||||
RCTAppState.getCurrentAppState(
|
||||
(appStateData) => {
|
||||
if (!eventUpdated) {
|
||||
@@ -129,15 +71,16 @@ class AppState extends NativeEventEmitter {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
// TODO: now that AppState is a subclass of NativeEventEmitter, we could
|
||||
// deprecate `addEventListener` and `removeEventListener` and just use
|
||||
// addListener` and `listener.remove()` directly. That will be a breaking
|
||||
// change though, as both the method and event names are different
|
||||
// (addListener events are currently required to be globally unique).
|
||||
/**
|
||||
* Add a handler to AppState changes by listening to the `change` event type
|
||||
* and providing the handler
|
||||
*
|
||||
* TODO: now that AppState is a subclass of NativeEventEmitter, we could deprecate
|
||||
* `addEventListener` and `removeEventListener` and just use `addListener` and
|
||||
* `listener.remove()` directly. That will be a breaking change though, as both
|
||||
* the method and event names are different (addListener events are currently
|
||||
* required to be globally unique).
|
||||
* and providing the handler.
|
||||
*
|
||||
* See http://facebook.github.io/react-native/docs/appstate.html#addeventlistener
|
||||
*/
|
||||
addEventListener(
|
||||
type: string,
|
||||
@@ -163,7 +106,9 @@ class AppState extends NativeEventEmitter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a handler by passing the `change` event type and the handler
|
||||
* Remove a handler by passing the `change` event type and the handler.
|
||||
*
|
||||
* See http://facebook.github.io/react-native/docs/appstate.html#removeeventlistener
|
||||
*/
|
||||
removeEventListener(
|
||||
type: string,
|
||||
@@ -200,9 +145,10 @@ if (__DEV__ && !RCTAppState) {
|
||||
}
|
||||
}
|
||||
|
||||
// This module depends on the native `RCTAppState` module. If you don't include it,
|
||||
// `AppState.isAvailable` will return `false`, and any method calls will throw.
|
||||
// We reassign the class variable to keep the autodoc generator happy.
|
||||
// This module depends on the native `RCTAppState` module. If you don't
|
||||
// include it, `AppState.isAvailable` will return `false`, and any method
|
||||
// calls will throw. We reassign the class variable to keep the autodoc
|
||||
// generator happy.
|
||||
AppState = new MissingNativeAppStateShim();
|
||||
} else {
|
||||
AppState = new AppState();
|
||||
|
||||
Reference in New Issue
Block a user