mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This PR tries to remove the code duplication between `RNTesterApp.ios.js` and `RNTesterApp.android.js`. The common code has been moved to `RNTesterAppShared.js`. This PR also refactors the state architecture and simplifies it to use just one single `useReducer` hook to manage the app's state. It also adds the visibility hack to make the screen change a little bit faster and removed the unneeded test files. Pull Request resolved: https://github.com/facebook/react-native/pull/29777 Reviewed By: cpojer Differential Revision: D23349302 Pulled By: rickhanlonii fbshipit-source-id: ce0d7b03e6ef65b1655bcb744817b73c5144438b
36 lines
815 B
JavaScript
36 lines
815 B
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import * as React from 'react';
|
|
import {AsyncStorage} from 'react-native';
|
|
|
|
import type {RNTesterState} from '../types/RNTesterTypes';
|
|
|
|
export const useAsyncStorageReducer = (
|
|
reducer: Function,
|
|
initialState: RNTesterState,
|
|
storageKey: string,
|
|
): [RNTesterState, Function] => {
|
|
const [state, dispatch] = React.useReducer<Function, Object>(
|
|
reducer,
|
|
initialState,
|
|
);
|
|
|
|
React.useEffect(() => {
|
|
if (state !== initialState) {
|
|
AsyncStorage.setItem(storageKey, JSON.stringify(state));
|
|
}
|
|
}, [state, storageKey, initialState]);
|
|
|
|
return [state, dispatch];
|
|
};
|