|
1 | | -import { NativeModules, DevSettings } from "react-native"; |
| 1 | +import { NativeModules, DevSettings } from 'react-native'; |
2 | 2 |
|
3 | 3 | // Tested with React Native 0.71 |
4 | | -if (window.__DEV__) { |
5 | | - DevSettings.addMenuItem("* Debug with Chrome", () => { |
6 | | - NativeModules.DevSettings.setIsDebuggingRemotely(true); |
7 | | - }); |
8 | | - DevSettings.addMenuItem("* Stop Debugging", () => { |
9 | | - NativeModules.DevSettings.setIsDebuggingRemotely(false); |
10 | | - }); |
| 4 | +const main = async () => { |
| 5 | + const message = { |
| 6 | + stop: '(*) Stop Debugging', |
| 7 | + debug: '(*) Debug JS Remotely', |
| 8 | + }; |
| 9 | + const storageKey = '@RNDS/isDebuggingRemotely'; |
| 10 | + try { |
| 11 | + const AsyncStorage = require('@react-native-async-storage/async-storage').default; |
| 12 | + const isDebuggingRemotelyString = await AsyncStorage.getItem(storageKey); |
| 13 | + let isDebuggingRemotely = isDebuggingRemotelyString === 'true'; |
| 14 | + DevSettings.addMenuItem(isDebuggingRemotely ? message.stop : message.debug, async () => { |
| 15 | + isDebuggingRemotely = !isDebuggingRemotely; |
| 16 | + |
| 17 | + await AsyncStorage.setItem(storageKey, JSON.stringify(isDebuggingRemotely)); |
| 18 | + NativeModules.DevSettings.setIsDebuggingRemotely(isDebuggingRemotely); |
| 19 | + }); |
| 20 | + } catch (error) { |
| 21 | + DevSettings.addMenuItem(message.debug, () => { |
| 22 | + NativeModules.DevSettings.setIsDebuggingRemotely(true); |
| 23 | + }); |
| 24 | + DevSettings.addMenuItem(message.stop, () => { |
| 25 | + NativeModules.DevSettings.setIsDebuggingRemotely(false); |
| 26 | + }); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +if (__DEV__) { |
| 31 | + // add a delay to avoid issue with React Native Debugger |
| 32 | + setTimeout(main, 100); |
11 | 33 | } |
0 commit comments