Issue #3420Opened April 28, 2021by sanchit360 reactions

[URGENT!!! QUESTION]: How to implement LocalStorage in reactjs project??

Question

I am trying to implement localstorage in reactjs app, it is for some reason not storing data in localstorage

 useEffect(() => {
    const templateComponent = JSON.parse(
      localStorage.getItem("gjs-components")
    );
    console.log(templateComponent);
    const templateStyle = JSON.parse(localStorage.getItem("gjs-style"));
    console.log(templateStyle);
    const editor = grapesjs.init({
      container: "#editor",
      width: "100vw",
      height: "100vh",

      styleManager: {
        clearProperties: true,
      },
      components: JSON.parse(localStorage.getItem("gjs-components")),
      style: JSON.parse(localStorage.getItem("gjs-style")),
      storageManager: {
        id: "gjs-",
        type: "local",
        autosave: true, 
        autoload: true, 
        stepsBeforeSave: 1,
      },
    });

Answers (1)

artfApril 28, 20210 reactions

The local storageManager does all the job for you, so you don't need to perform all those manual stuff you're doing now. So leave the storageManager option and remove the rest.

ps. be careful with useEffect, add an empty array as dependency or it will recall the init on each render...

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.