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

The difference and introduction of ARGB, RGB and RGBA

ARGB is a color mode, which is the RGB color mode...

How to implement a multi-terminal bridging platform based on websocket in JS

Table of contents 1. What to debug 2. Features of...

MySQL query data by hour, fill in 0 if there is no data

Demand background A statistical interface, the fr...

Web Design: The Accurate Location and Use of Massive Materials

Three times of memorization allows you to remembe...

How Database SQL SELECT Queries Work

As Web developers, although we are not profession...

How to detect Ubuntu version using command line

Method 1: Use the lsb_release utility The lsb_rel...

How to design and optimize MySQL indexes

Table of contents What is an index? Leftmost pref...

Notes on matching MySql 8.0 and corresponding driver packages

MySql 8.0 corresponding driver package matching A...

MySQL Index Optimization Explained

In daily work, we sometimes run slow queries to r...

Summary of 10 must-see JavaScript interview questions (recommended)

1.This points to 1. Who calls whom? example: func...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

About the use of Vue v-on directive

Table of contents 1. Listening for events 2. Pass...

About WeChat Mini Program to implement cloud payment

Table of contents 1. Introduction 2. Thought Anal...