My Code Docs

My Code Docs

  • Docs
  • Projects
  • Components
  • Help

›Javascript

Javascript

  • Promises and Asnyc/Await
  • Local Storage
  • Firebase
  • JS Language Basics
  • JS Array Functions
  • Keyboard and Mouse Input
  • ES6 Cheatsheet
  • ESLint Setup
  • Data Structures
  • Naming Conventions
  • Javascript Resources
  • Javascript Snippets
  • npm Module Creation

Node

  • Node JS Basics
  • Server Config and SSH

NextJS

  • NextJS Basics

JS/React Libs

  • Lodash Library
  • Axios
  • Ramda
  • Moment JS
  • Overmind
  • Redux Forms npm Module
  • React Beautiful DnD
  • Ant Design
  • Zustand State Managment

React

  • React Basics
  • React Hooks
  • React With Classes
  • Reach Router
  • React Router
  • React Components

Redux

  • Redux in React
  • My Redux Pattern
  • Redux with React (First Doc)
  • Simple Redux Package

ReactNative

  • React Native
  • React Navigation V5
  • React Navigation V4

HTMLCSS

  • HTML and CSS Snippets
  • Tailwind CSS

Electron

  • Electron React Boiler
  • Electron Basics
  • Electron Packaging
  • Electron Tooling

Using Local Storage

Local Storage is a browsers own data store. I have only used in chrome, assume it exists in other browsers.

Main thing to know is that Local Storage is stored in JSON format. So, if you have a JavaScript object, make sure to use JSON.stringify on it before saving to Local Storage and also when reading from Local Storage, be sure to use JSON.parse to put into JS Object.

Below is a sample of using the **localStorage.getItem() ** function

function loadData() {
    var stringtvShows = localStorage.getItem('tvShows');
    var tvShows = [];

    try {
    if ( tvShows === null ) {
        return [];
    } else {
        tvShows = JSON.parse(stringtvShows);
    }
    } catch (e) {
        alert("error loading TVShows from localStorage: ", e);
    }
    return Array.isArray(tvShows) ? tvShows : [];
},

Below is a sample of saving data using localStorage.setItem()

function saveData (tvShows, showData) {

    if (Array.isArray(tvShows)) {
        localStorage.setItem('tvShows', JSON.stringify(tvShows));
        localStorage.setItem('showData', JSON.stringify(showData));
    }
}

A few other useful functions:

localStorage.removeItem('itemName') - removes the item from Local Storage.

← Promises and Asnyc/AwaitFirebase →
My Code Docs
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright © 2020 McCoidCo