Skip to content

Commit

Permalink
Add clearer comment about headless Onyx init
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Aug 27, 2021
1 parent 7179916 commit b9a9606
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ import platformSetup from './platformSetup';
import {canCaptureOnyxMetrics} from '../libs/canCaptureMetrics';

export default function () {
// Initialize the Onyx store when the app loads for the first time.
// Note: This Onyx initialization has been very intentionally placed outside of the React lifecycle of the main App component.
// This enables us to use Onyx in a headless JS context (i.e: to update Onyx data in response to a push notification received when the app is completely killed).
// More info here: https://github.com/transistorsoft/react-native-background-fetch/issues/165#issuecomment-514255928
/*
* Initialize the Onyx store when the app loads for the first time.
*
* Note: This Onyx initialization has been very intentionally placed completely outside of the React lifecycle of the main App component.
*
* To understand why we must do this, you must first understand that a typical React Native Android application consists of an Application and an Activity.
* The project root's index.js runs in the Application, but the main RN `App` component + UI runs in a separate Activity, spawned when you call AppRegistry.registerComponent.
* When an application launches in a headless JS context (i.e: when woken from a killed state by a push notification), only the Application is available, but not the UI Activity.
* This means that in a headless context NO REACT CODE IS EXECUTED, and none of your components will mount.
*
* However, we still need to use Onyx to update the underlying app data from the headless JS context.
* Therefore it must be initialized completely outside the React component lifecycle.
*/
Onyx.init({
keys: ONYXKEYS,
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
Expand Down

0 comments on commit b9a9606

Please sign in to comment.