Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 1.38 KB

useStorage.md

File metadata and controls

40 lines (29 loc) · 1.38 KB

useStorage

An utility to quickly create hooks to access both Session Storage and Local Storage.

💡 Why?

import React, { useCallback } from 'react'; 
import { Pill, Paragraph, Icon } from 'beautiful-react-ui'; 
import { useStorage } from 'beautiful-react-hooks'; 

const NotificationBadgeExample = ({ notifications }) => {
  const useLocalStorage = useStorage('local');
  const [notificationCount, setNotificationCount] = useLocalStorage('demo-notification-count', notifications);
  
  const clearNotifications = useCallback(() => {
    setNotificationCount(0);
  }, [notificationCount]);

  return (
    <DisplayDemo>
        <Paragraph>Click on the badge to clear from the local storage</Paragraph>
        <Pill color="primary" onClick={clearNotifications}>
          <Icon name="envelope" />
          You've got {notificationCount} new messages
        </Pill>
    </DisplayDemo>
  )
};

<NotificationBadgeExample notifications={100}/>

Mastering the hooks

✅ When to use