4

The Problem

I am migrating the logout functionality of my hybrid app from native to react native.

Requirements:

  • It works in production and on a device
  • It navigates to the root screen of the app (login screen)
  • It clears the redux store
  • [nice to have] It cancels any in-flight requests

Initially, we planned to do something like this: how-to-reset-the-state-of-a-redux-store

The Idea

As of React Native 0.62.0, we now have access to the DevSettings module. DevSettings has a native bridge that can reload the react-native environment. But are DevSettings really only for development environments?

Export the DevSettings module, add addMenuItem method (cc068b0551 by @janicduplessis)

The Question

  1. What are the tradeoffs for using reload vs logging out with a redux action?
  2. Should I use reload in a production app?

Edit: The Answer

  1. Reload is disallowed in prod (code)
  2. react-native-restart worked great for my use case.
Tai
  • 490
  • 4
  • 18

2 Answers2

4

Here's the line in code that disallows using devSettings in prod

import NativeDevSettings from '../NativeModules/specs/NativeDevSettings';
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';

class DevSettings extends NativeEventEmitter {
  ...
  reload(reason: string) {
    if (typeof NativeDevSettings.reloadWithReason === 'function') {
      NativeDevSettings.reloadWithReason(reason || 'Uncategorized from JS');
    } else {
      NativeDevSettings.reload();
    }
  }
  ...
}

// Avoid including the full `NativeDevSettings` class in prod.
class NoopDevSettings {
  addMenuItem(title: string, handler: () => mixed) {}
  reload() {}
}

module.exports = __DEV__ ? new DevSettings() : new NoopDevSettings();

from Libraries/Utilities/DevSettings

Tai
  • 490
  • 4
  • 18
1

The DevSettings module exposes methods for customizing settings for developers in development.

The DevSettings is for development environment only, it will not work in release mode. The best solution is to follow the link you shared in your question

Mahdi N
  • 2,128
  • 3
  • 17
  • 38
  • Ah yes, I read that in the docs but the docs are pretty sparse. Why is it only in development? Does it enforce it can only be used in development? – Tai Apr 23 '20 at 18:10
  • It's just a new way to reload your app with a user interaction but it's not a possibility to reload an app in release – Mahdi N Apr 23 '20 at 18:21
  • Can you elaborate? Where in the code does it say it is not possible? edit: found something https://github.com/facebook/react-native/blob/0b9ea60b4fee8cacc36e7160e31b91fc114dbc0d/Libraries/Utilities/DevSettings.js#L59-L65 – Tai Apr 23 '20 at 20:07