Files
react-native/packages/rn-tester/js/utils/useAsyncStorageReducer.js
T
Riccardo Cipolleschi ecf3293985 Back out "Remove AsyncStorage from rn-tester and fix InternalSettings Example"
Summary:
This diff breaks the RNTester app at runtime and it prevents us from cutting RN 0.69.

Original commit changeset: a879787d8683

Original Phabricator Diff: D35435562 (https://github.com/facebook/react-native/commit/f4c4f446e4365c6cf7adf5db21d3d746b38aa3e2)

## Changelog
[General][Fixed] - Revert change which breaks RNTester

Reviewed By: cortinico, kacieb

Differential Revision: D35936298

fbshipit-source-id: c4ec92002fde9c1de691bcda17ebc5d0747914f6
2022-04-27 01:10:18 -07:00

34 lines
802 B
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and 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
*/
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];
};