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.

Revisiting Hooks implementation

See original GitHub issue

You want to:

Discuss required changes to current ‘hook-ish’ implementation for Hooks support.

Details:

As you can see here, we’ve got a nice wrapper on top AsyncStorage, to mimic hooks usage. This discussion here is to come up with better and valid implementation, that leverages hooks API.

Example implementation could look like this:

function useAsyncStorage(key, defaultValue) {
  const [storageValue, updateStorageValue] = useState(defaultValue);

  useEffect(() => {
    getStorageValue();
  }, []);

  async function getStorageValue() {
    let value = defaultValue;
    try {
      value = (await AsyncStorage.getItem(key)) || defaultValue;
    } catch (e) {
      // handle here
    } finally {
      updateStorageValue(value);
    }
  }

  async function updateStorage(newValue) {
    try {
      await AsyncStorage.setItem(key, newValue);
    } catch (e) {
      // handle here
    } finally {
      getStorageValue();
    }
  }

  return [storageValue, updateStorage];
}

The problem in here is that, once we got a value inside AsyncStorage, we’ll be getting two rerenders one component mounting - one returning the default value, second the actual stored data.

I’m more than happy to discuss this, so please let me know what you think.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
edwinvrgscommented, Mar 21, 2019
const useAsyncStorage = (key, defaultValue) => {
  const [storageValue, updateStorageValue] = useState(defaultValue);
  const [updated, setUpdated] = useState(false);

  async function getStorageValue() {
    let value = defaultValue;
    try {
      value = JSON.parse(await AsyncStorage.getItem(key)) || defaultValue;
    } catch (e) {
    } finally {
      updateStorageValue(value);
      setUpdated(true);
    }
  }

  async function updateStorage(newValue) {
    try {
      if (newValue === null) {
        await AsyncStorage.removeItem(key);
      } else {
        const value = JSON.stringify(newValue);
        await AsyncStorage.setItem(key, value);
      }
    } catch (e) {
    } finally {
      setUpdated(false);
      getStorageValue();
    }
  }

  useEffect(() => {
    getStorageValue();
  }, [updated]);

  return [storageValue, updateStorage];
};
7reactions
krizzucommented, Jul 4, 2019

We’re going to revisit it in v2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Revisiting React Hooks. Originally published at… - Medium
In this example we will take a closer look at the two of the most important built-in hooks — useState and useEffect.
Read more >
Deep dive: How do React hooks really work? - Netlify
In this article, we reintroduce closures by building a tiny clone of React Hooks. This will serve two purposes - to demonstrate the ......
Read more >
React Hooks Revisited: Introduction - DEV Community ‍ ‍
Introduced in React v16.8, the hooks API represents a change in how developers compose their components. Intended to compartmentalize blocks ...
Read more >
Understanding common frustrations with React Hooks
What problems do React Hooks solve? · What was wrong with class components? · Yet another JavaScript paradigm to learn · To hell...
Read more >
Revisiting Hooks — sourcehut lists
Revisiting Hooks == Context After implementing the initial work for storage hooks based on RFC 703[0], the next task was to wire up...
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