score:2

Accepted answer

all you need is to save the necessary data with a timestamp. and check whether the date has been expired.

loaddata = () => {
    // check whether data exists in localstorage and has been
    // saved less than an hour ago
    const data = json.parse(localstorage.getitem('brand'));
    if (data && (new date().gettime() - data.created <= 3600)) {
        this.setstate({
            note: data.brand.note,
            logo: data.brand.logo,
            name: data.brand.name,
        });

        return;
    }

    // otherwise perform request
    axios.get('https://myapi.url').then(res => {
        const brand = res.data;

        // and save data in local storage with the current timestamp
        localstorage.setitem(
            'brand',
            json.stringify({
                created: new date().gettime(),
                brand,
            }),
        );

        this.setstate({
            note: brand.note,
            logo: brand.logo,
            name: brand.name,
        });
    });
};

score:0

    // save to localstorage
    const data = {
            "name": "my name",
            "logo": "https://media.img.jpeg",
            "note": "14,5",
            "students": 3720,
            "url": "https://url.students/alain"
    }
    localstorage.setitem('key', json.stringify(data))


    // to get data from localstorage 
    const data = json.parse(localstorage.getitem('key'));
    console.log(data);


    // to remove data
    localstorage.removeitem('key');
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

score:0

instead of saving these items to your localstorage, i recommend to store them in the redux store instead. if you're not using redux: https://react-redux.js.org/introduction/quick-start


Related Query

More Query from same tag