1.0.21 • Published 2 years ago

@minimal_ui/save_data v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@minimal_ui/save_data

Save all data in localstorage. For example http api response, previous selected theme, login related info, etc.

NPM JavaScript Style Guide

Install

npm install --save @minimal_ui/save_data

How it works

It saves all the data in the localstorage. 
For every request to udpate the data it checks if the data is already stored or not.
If the data is present then it returns the saved data.
Else it calls the method to refresh the data.

Usage

// Root component
import React from 'react'
import { Provider } from '@minimal_ui/save_data';
import { List } from './List';

export default () => {
  // Add provider to parent component
  return (
    <Provider>
      <List />
    </Provider>
  );
}

// List component
import React, { useContext } from "react";
import { Context } from '@minimal_ui/save_data'

export const List = () => {

    const { updateStore, store } = useContext(Context);
  
    // Fetch the list of data
    async function fetchUsers() {
        const res: any = await fetch("https://reqres.in/api/users?page=2");
        return res.json();
    }
  
    function onClick() {
        updateStore({
            // Method that will fetch the list of data
            fetcher: fetchUsers,
            // Any unique name
            url: "fetch-users",
            // boolean to check for update(default = true)
            checkForUpdate: false
        });
    }

    return (
      <div>
        <button onClick={onClick}>Fetch Users</button>
        {store?["fetch-users"]?.data?.map((item: any) =>{
            return (
                <div key={item.id}>{item.email}</div>
            );
        })}
      </div>
    );
  
  }

License

MIT © singh-taranjeet

1.0.21

2 years ago

1.0.20

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago