-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoot.tsx
38 lines (32 loc) · 1.21 KB
/
Root.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { useCallback, useEffect, useState } from 'react';
import { DevSettings } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Storybook from './storybook';
import { App } from './App';
const setPersistedStorybookActive = async (storybookActive): Promise<void> => {
AsyncStorage.setItem('storybookActive', storybookActive ? 'true' : 'false');
};
export const Root = (): any => {
const [storybookActive, setStorybookActive] = useState(false);
const toggleStorybook = useCallback(
() => {
setPersistedStorybookActive(!storybookActive);
setStorybookActive(!storybookActive);
},
[storybookActive],
);
// read value from local storage so that storybook toggle persists through reloads
useEffect(() => {
const getPersistedStorybookActive = async (): Promise<void> => {
const value = await AsyncStorage.getItem('storybookActive');
setStorybookActive(value === 'true');
};
getPersistedStorybookActive();
}, [storybookActive]);
useEffect(() => {
if (__DEV__) {
DevSettings.addMenuItem('Toggle Storybook', toggleStorybook);
}
}, [toggleStorybook]);
return storybookActive ? <Storybook /> : <App />;
};