Solve the pitfall of storing boolean type values ​​in localstorage

Solve the pitfall of storing boolean type values ​​in localstorage

LocalStorage stores Boolean values

Today, when I used localstorage to store boolean data, I found that there was a problem with how to display the data on the page.

Later I found that the boolean data stored in localstorage was converted into strings, which caused this problem.

Therefore, "true"=true, "false"==false, and "true"==false are both displayed as false.

The pitfalls of not using localstorage rigorously

After launching the new version, we found that a very small number of "old" users were unable to open the homepage of our website in the WeChat browser. After an online file proxy

After changing it, I finally found the problem.

Problem code snippet:

if (localstorage.getItem("things")) {
    var things = localstorage.getItem("things");
    use(things);
    // Delete the cache after using it once localstorage.removeItem('things');
}else{
    use(newData);
}

This code may seem fine at first glance, but it has hidden dangers. In the old version, the contents of things stored in localstorage are as follows:

{
    name:'px',
    age:'25'
}

However, in the new version, due to demand issues, the value of this cache has changed to the following structure:

{
    username:'px',
    myage:'25'
}

This results in an error when using the use function to process things, causing the subsequent removeItem to never be executed, so the cached data is never cleared in the code, and the use function always uses the old data for rendering, which results in an error and the new data can never be used.

There are two points that need improvement

* Add a version number to the cache * Clear the cache immediately after reading the cache with a variable

The optimized code is as follows:

//First determine the cache version number if (localstorage.getItem ("version") == curVersion) {
    if (localstorage.getItem("things")) {
        var things = localstorage.getItem("things");
        //Clear immediately localstorage.removeItem('things');
        use(things);
    }else{
        use(newData);
    }
}else{
    localstorage.removeItem('things');
    use(newData);
}

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of local storage usage
  • JavaScript uses localStorage to store data
  • How to use localStorage to store data in Vue

<<:  How to install and use Cockpit on CentOS 8/RHEL 8

>>:  Introduction to the properties of B-Tree

Recommend

Detailed tutorial on how to automatically install CentOS7.6 using PXE

1. Demand The base has 300 new servers, and needs...

Tutorial on building a zookeeper server on Windows

Installation & Configuration The official web...

MySQL Quick Data Comparison Techniques

In MySQL operation and maintenance, a R&D col...

How to enable the slow query log function in MySQL

The MySQL slow query log is very useful for track...

Methods and techniques for designing an interesting website (picture)

Have you ever encountered a situation where we hav...

Detailed explanation of custom instructions for Vue.js source code analysis

Preface In addition to the default built-in direc...

MySQL quickly inserts 100 million test data

Table of contents 1. Create a table 1.1 Create te...

Recommend a cool interactive website made by a front-end engineer

Website link: http://strml.net/ By Samuel Reed Ti...

Alibaba Cloud Centos7 installation and configuration of SVN

1. Install SVN server yum install subversion 2. C...

Complete steps to configure IP address in Ubuntu 18.04 LTS

Preface The method of configuring IP addresses in...

Linux echo text processing command usage and examples

The description of echo in the Linux help documen...

CSS to achieve Tik Tok subscription button animation effect

I was watching Tik Tok some time ago and thought ...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

XHTML Getting Started Tutorial: XHTML Tags

Introduction to XHTML tags <br />Perhaps you...