Coder Social home page Coder Social logo

Comments (6)

dszlachta avatar dszlachta commented on August 15, 2024 1

I think the easiest way is to use useEffect to save the variables from App.tsx into localStorage. Other option would be to do the same, but with wrapping them in Context. I think at some point we will want to do the latter. In both cases the logic would be very similar:

const saveToLocalStorage = (key, data) => {
  try {
    window.localStorage.setItem(key, JSON.stringify(data));
  } catch (error) {
    console.error(error); // this will not throw, but still show the error in the console
  }
}

const loadFromLocalStorage  = (key, defaultValue) => 
  const loadedData = window.localStorage.getItem(key);
  return loadedData ?? defaultValue; // ?? operator returns right hand side only if left side is null or undefined
}

// useState with function as an initial value will run only once, which is great, because reading/writing
// to LS is blocking
const [helpExpanded, setHelpExpanded] = useState(() => loadFromLocalStorage('helpExpanded'));

// The function inside will be called each time a variable in the list of dependencies (the array being the last argument)
// is changed
useEffect(() => {
  saveToLocalStorage('helpExpanded', helpExpanded);
}, ['helpExpanded']);

// Just send the state as a prop to the component
<HelpPanel expanded={helpExpanded} />

This is basically the same code, but with Context: https://stackoverflow.com/a/62505656

from trueblocks-explorer.

tjayrush avatar tjayrush commented on August 15, 2024

All I need here is a recommendation or pointer to how to best do this.

from trueblocks-explorer.

tjayrush avatar tjayrush commented on August 15, 2024

Sorry to be so ignorant, but where does all this code go? I'd like to make these changes, and I fully understand the code, but I'm very unsure where to put it.

from trueblocks-explorer.

dszlachta avatar dszlachta commented on August 15, 2024

It is OK. I would put the two functions (saveToLocalStorage and loadFromLocalStorage) into a module in src/modules. It could be named e.g. local_storage.ts. This way we have these nice wrappers available whenever we need localStorage access. The rest (state and useEffect) could go to App.tsx.

from trueblocks-explorer.

deeayeen avatar deeayeen commented on August 15, 2024

#61

from trueblocks-explorer.

tjayrush avatar tjayrush commented on August 15, 2024

Thanks Dan.

from trueblocks-explorer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.