Comments (6)
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.
All I need here is a recommendation or pointer to how to best do this.
from trueblocks-explorer.
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.
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.
from trueblocks-explorer.
Thanks Dan.
from trueblocks-explorer.
Related Issues (20)
- Hitting escape should stop loading
- A single skeleton row should appear while loading, but selection should be on second-to-last row
- Last row of transaction history shows no event HOT 1
- If event filter is on, show only those events in the transaction history right-side detail panel
- Export option does not work
- Memory leak? HOT 1
- Blank screen when first running explorer -- because of missing setting in .env file HOT 3
- Feature suggestion: GUI authentication HOT 7
- Note
- Various explorer issues
- Removed 'slowQueries', but we still need something
- Page never loads HOT 15
- chifra names autonamer doesn't work HOT 1
- Show sender and receiver in display
- I cannot install HOT 4
- nice design - don't steal it, be inspired by it
- showing status
- Feature suggestion: Docker version of trueblocks-explorer HOT 1
- Explorer: First Run of App / Error Modes HOT 2
- Explorer: Open issues with Onboarding
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trueblocks-explorer.