Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Using localstorage dynamically in react

I'm brand new to react, so bare with me.

I have some data set in local storage before i start my react application, and i want to dynamically loop through local storage and display the data.

Currently I have it explicitly set:

import React from 'react';

const App = () => {
    return (
        <div>
            <h1>Hello World</h1>
            <ul>
                <li>locale: {localStorage.getItem('locale')}</li>
                <li>pubKey: {localStorage.getItem('pubKey')}</li>
                <li>version: {localStorage.getItem('version')}</li>
                <li>dist: {localStorage.getItem('dist')}</li>
            </ul>
        </div>
    );
}

export default App;

but i want to not do explicit definitions, but loop through the localstorage data this dynamically.

elegant and complex solutions always welcome as well.

UPDATE::

I've tried:

in html:

<script>
window.localStorage.setItem('app:data:persist',JSON.stringify({"version":"0.0.1","dist":"shard","locale":"en_US","pubKey":"6asd68d68ddd6saadd79asd7das79ads9"}));</script></head>
<body>
const App = () => {
    const localStorageData = JSON.parse(window.localStorage.getItem('app:data:persist'));
    return (
        <div>
            <h1>Hello World</h1>
            <ul>
                {localStorageData.map((data,key) => {
                    return (
                        <li>{key} : {data[key]}</li>
                    )
                })};
            </ul>
        </div>
    );
}

but i cant get the value for the entry with the key correctly?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's not clear about handle this dynamically and explicit definitions. Please clarify. If you use Redux, you can use https://github.com/rt2zz/redux-persist. Load the cached data into the redux store from the store engine when the application initializes.

If you don't want to use redux, just use one cache key for the whole cached data of the application.

Also, consider using React context to hold the cached data, so that nested components can access the cached data conveniently.

E.g.

localStorage.setItem('myapp:1.0.0:persist', JSON.stringify({ list, ... }));

const data = JSON.parse(localStorage.getItem('myapp:1.0.0:persist'));
data.list.map(...)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda