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

Feature: resetState method #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

donbader
Copy link

@donbader donbader commented May 23, 2021

The resetting method would be very useful when we want to reset the state to the initial values
in the scenario where we want to log out an user or cleanup the data

  • tests covered

Usage

[myData, setMyData, restored, resetMyData] = usePersistStorage('@TheKey', () => { 'MyData' })
console.log(myData); // 'My data'

setMyData('New data')
console.log(myData); // 'New data'
setMyData('Other data')
console.log(myData); // 'Other data'

resetMyData();
console.log(myData); // 'My data'

@donbader donbader changed the title Feature: Add resetState method Feature: resetState method May 23, 2021
@TseHang
Copy link
Collaborator

TseHang commented May 23, 2021

Thanks @donbader , could you also help to update readme ?

initialValue instanceof Function
? initialValue()
: initialValue;
await asyncSetState(newValue);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be better if we can wrap useCallback, and reduce the dependency.
I will suggest you re-write to like below...

const resetState = useCallback(async () => {
    const newValue: Value =
      initialValue instanceof Function
        ? initialValue()
        : initialValue;
  
    setState(newValue);
    if (persist) {
      await setValueToStorage(newValue);
    }
  }, [initialValue, persist, setValueToStorage]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants