question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using custom storage engine on React Native

See original GitHub issue

For some reason I cannot seem to be able to make my custom storage engine working on RN. What works in a clean CRA web project doesn’t in a clean CRNA project.

I have created a new project by running expo init and choosing the Bare workflow with minimal settings. I’ve installed the easy-peasy package (4.0.1) and modified the App.js as such:

import React from "react";
import { Button, StyleSheet, View } from "react-native";
import {
  action,
  createStore,
  persist,
  StoreProvider,
  useStoreActions
} from "easy-peasy";

const store = createStore(
  persist(
    {
      count: 1,
      add: action(state => {
        state.count += 1;
        console.log("STATE CHANGE", state.count);
      })
    },
    {
      storage: {
        getItem: key => {
          console.log("GET_ITEM", key);
        },
        setItem: (key, value) => {
          console.log("SET_ITEM", key, value);
        },
        removeItem: key => {
          console.log("REMOVE_ITEM", key);
        }
      },
      allow: ["count"]
    }
  )
);

const MyButton = () => {
  const add = useStoreActions(actions => actions.add);
  return <Button title="Press" onPress={() => add()} />;
};

export default function App() {
  return (
    <StoreProvider store={store}>
      <View style={styles.container}>
        <MyButton title="Press" />
      </View>
    </StoreProvider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center"
  }
});

As I launch the project in my iOS simulator, GET_ITEM [EasyPeasyStore][0] is logged. As I press the button, STATE CHANGE 2 is logged. However, SET_ITEM never gets logged.

As mentioned, the same code on a web project works fine and SET_ITEM... is logged.

Any ideas?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
duygiangdgcommented, Feb 18, 2021

This is caused by an issue of RN 0.62.2, so requestIdleCallback is never called on iOS.

Before it is fixed, you can add this line before your createStore(), so easy-peasy will use requestAnimationFrame instead of requestIdleCallback

window.requestIdleCallback = null;

5reactions
yixuan98commented, Nov 10, 2020

Same issue after upgrading to v4.0.1, setItem() of custom storage isn’t called when changing persisted state on RN 0.63.3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DataStore - How it works - React Native - AWS Amplify Docs
The Storage Engine manages a "Model Repository" of Models which were defined by the developer's GraphQL schema as well as "System Models" which...
Read more >
Secure Storage in React Native - Randy Coulman
redux-persist allows for custom storage engines. As long as a storage engine conforms to the correct API, it can be plugged into redux-persist ......
Read more >
What are my options for storing data when using React Native ...
With react native, you probably want to use redux and redux-persist. It can use multiple storage engines. AsyncStorage and redux-persist- ...
Read more >
MMKV – Fast and Encrypted React Native Storage - Netguru
MMKV is an efficient and reliable storage framework for React Native. Read our handy article for info on how to install and use...
Read more >
Choosing the right database for your React Native app - Medium
React Native is compatible with a variety of local databases, such as: ... Realm has its own database engine and doesn't just rely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found