Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-use-stored-state

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-use-stored-state

State hook for react-native and react-native-web that allows easy usage of state that is persisted. It is a wrapper of async-storage which simplifies its usage for objects and loading states.

  • 0.2.7
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by700%
Maintainers
1
Weekly downloads
 
Created
Source

react-native-use-stored-state

State hook for react-native and react-native-web that allows easy usage of state that is persisted. It is a wrapper of @react-native-async-storage/async-storage which makes it easier to deal with objects and loading states.

Installation:

yarn add react-native-use-stored-state

Usage:

const reducer = (prevState, action) => {
  switch (action.type) {
    case 'age':
      return {...prevState, active: action.payload.age };
  }
}

const MyComponent = () => {
  const [myName, setMyName, myNameIsLoaded, unsetName] = useStoredState('NAME_KEY_FOR_ASYNC_STORAGE');
  const [myState, dispatch, myStateIsLoaded, unsetMyState] = useStoredReducer('MY_STATE_KEY_FOR_ASYNC_STORAGE', reducer, {
    active: false,
  });

  if (!myNameIsLoaded || !myStateIsLoaded) {
    return <Loading />;
  }

  const onNameChange = (name) => {
    setMyName(name);
  };

  const onStateChange = (key, value) => {
    dispatch({
      type: 'abc',
      payload: {
        [key]: value,
      }
    });
  };

  const onClear = () => {
    unsetName();
    unsetMyState();
  };

  return (
    <Container>
      <Input value={myName} onChange={onNameChange} />
      <Checkbox value={myState.active} onChange={(value) => onStateChange('active', value)} />
      <Input value={myState.age} onChange={(value) => onStateChange('age', value)} />
       <Input value={myState.year} onChange={(value) => onStateChange('year', value)} />
      <Button onPress={onClear}>Clear</Button>
    </Container>
  );
}

this in typescript would be:

interface MyState {
  active: boolean;
  age?: number;
}

const ReducerAction = 'age' | 'year';

const reducer = (prevState: MyState, action: ReducerAction): MyState => {
  switch (action.type) {
    case 'age':
      return {...prevState, active: action.payload.age };
  }
}

const MyComponent = (): ReactElement => {
  const [myName, setMyName, myNameIsLoaded] = useStoredState<string>('NAME_KEY_FOR_ASYNC_STORAGE');
  const [myState, setMyState, myStateIsLoaded] = useStoredReducer<MyState, ReducerAction>('MY_STATE_KEY_FOR_ASYNC_STORAGE', reducer, {
    active: false,
  });

  if (!myNameIsLoaded || !myStateIsLoaded) {
    return <Loading />;
  }

  const onNameChange = (name: string) => {
    setMyName(name);
  };

  const onStateChange = <K extends keyof MyState>(key: K, value: MyState[K]) => {
    dispatch({
      type: 'abc',
      payload: {
        [key]: value,
      }
    });
  };

  const onClear = () => {
    unsetName();
    unsetMyState();
  };

  return (
    <Container>
      <Input value={myName} onChange={onNameChange} />
      <Checkbox value={myState.active} onChange={(value) => onStateChange('active', value)} />
      <Input value={myState.age} onChange={(value) => onStateChange('age', value)} />
       <Input value={myState.year} onChange={(value) => onStateChange('year', value)} />
      <Button onPress={onClear}>Clear</Button>
    </Container>
  );
};

FAQs

Package last updated on 30 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc