Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useAsyncStorage): make more robust #760

Closed

Conversation

pke
Copy link

@pke pke commented Feb 26, 2022

Summary

Fixes the broken useAsyncStorage and introduces a new useAsyncStorageValue as a true hook replacement.

I think we should not change the signature of useAsyncStorage for now in a patch release.
In this patch release we could also already introduce the new useAsyncStorageValue which will be renamed to useAsyncStorage in the next major version.

I'll also add watch support to make the hook truly magic and work across multiple components.
I'll first add the watch support to the hooks infrastructure only, that means any changes done by AsyncStorage.setXXX/mergeXXX are not detected. But when one only uses the hooks, then changes in values are propagated into each hook.

Test Plan

yarn jest __tests___ for now.

@pke
Copy link
Author

pke commented Feb 26, 2022

The current jest tests in the example folder don't seem to work.

I'd suggest to treat the example app as its own project with own package file instead.

The libs dependency should not carry an dependencies/tests the example project needs.

src/hooks.ts Outdated Show resolved Hide resolved
@krizzu
Copy link
Member

krizzu commented Mar 29, 2022

Hey @pke, do you need help with anything?

package.json Outdated Show resolved Hide resolved
@pke pke force-pushed the fix/useAsyncStorage branch 2 times, most recently from 0cda315 to c0a06f6 Compare March 29, 2022 15:33
@pke
Copy link
Author

pke commented Mar 29, 2022

Updated. Removed code not required for this PR.

Note: The test command does not run successfully locally, it shows many TS linting errors in the App/example tests.

@pke pke marked this pull request as ready for review March 29, 2022 15:38
Copy link
Member

@tido64 tido64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the hooks! I've left some comments.

package.json Outdated Show resolved Hide resolved
src/hooks.ts Outdated
);

const setItem = React.useCallback(
//@ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add any @ts-ignore. What's the error here? Can we fix it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes its required because TS complains:
[ts] src/hooks.ts(11,44): error TS2556: A spread argument must either have a tuple type or be passed to a rest parameter.

src/hooks.ts Outdated

const mergeItem = React.useCallback(
(...args) =>
//@ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add any @ts-ignore. What's the error here? Can we fix it?

const setItem = React.useCallback(
//@ts-ignore
(...args) => AsyncStorage.setItem(key, ...args),
[key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to create a new array for each of the callbacks? Can we create it once and reuse?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this doesn't work. Eslint will complain:

const dep = [key]
const getItem = React.useCallback(
  (...args) => AsyncStorage.getItem(key, ...args),
  dep
);
const dep: string[]

React Hook React.useCallback was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies.eslint[react-hooks/exhaustive-deps](https://github.com/facebook/react/issues/14920)
React Hook React.useCallback has a missing dependency: 'key'. Either include it or remove the dependency array.eslint[react-hooks/exhaustive-deps](https://github.com/facebook/react/issues/14920)

yarn.lock Show resolved Hide resolved
/**
* @format
*/
/* eslint-disable no-shadow */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this ESLint disable necessary?

@pke
Copy link
Author

pke commented Apr 9, 2022

After doing some more jest testing in my own project I came to the conclusion that the RNAS tests are somewhat broken. Maybe the mock is broken. I need to investigate more.

@pke
Copy link
Author

pke commented May 24, 2022

I have not much time to work on that further, since I also moved to an alternative sync lib for settings.

The tests for the hooks don't seem to run, not sure whats wrong with the setup there. Complaining about not able to find the RNAS mock.

@github-actions
Copy link

This PR has been marked as stale due to inactivity. Please address any comments within 7 days or it will be closed.

@github-actions github-actions bot added the Stale label Jul 24, 2022
@github-actions github-actions bot closed this Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants