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

382
Visualizações
React - Display all items inside localStorage as a Material UI List

I have localStorage with multiple items on it. I want to retrieve all of them and display it on a <ListItem> Material UI.

Here's my current code:

function saveJob(key, value) {
  localStorage.setItem(key, value);
}

function saveJob is basically just save the value along with unique key. The content of the localStorage would be something like this:

Key Value
1 Technician
2 Plumber

How I retrieved the items back:

var savedJobs = [];
useEffect(() => {
    var keys = Object.keys(localStorage),
        i = keys.length;

    while (i--) {
        savedJobs.push(localStorage.getItem(keys[i]));
    }

    return savedJobs;
}, [])

Now onto the problem, I tried to display it in a functional component through <ListItem> Material UI like the following:

<List>
    {savedJobs.map((job) => (
    <ListItem key={job.Key} disablePadding>
    <ListItemButton>
      <ListItemText  primary={job.Value} />
    </ListItemButton>
  </ListItem>
    ))}
</List>

This is not working. Can anyone point me in the right direction?

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

0

React doesn't trigger rerender when a local variable in a function component was changed. It will be rerendered when: the state has changed, props were changed or the parent component was rerendered.

You should put your savedJobs to state and write

const [savedJobs, setSavedJobs] = useState([]);

instead of

var savedJobs = [];

about 4 years ago · Juan Pablo Isaza Relatório

0

You need state to store the data you retrieve from localstorage. Instead of your savedJobs var outside of your useEffect, create an array inside the hook and then update your state with this data. Here's an example:

  // Initiate your state as an empty array.
  const [savedJobs, setSavedJobs] = useState([]);

  // Used as an example instead of localstorage.
  const example = ["foo", "bar"];

  useEffect(() => {
    let jobs = [];
    let keys = Object.keys(example),
      i = keys.length;
    while (i--) {
      jobs.push(example[i]);
    }
    setSavedJobs(jobs);
  }, [example]);

  return <>{savedJobs.map((job) => job)}</>;

Or see this interactive example.

about 4 years ago · Juan Pablo Isaza Relatório

0

First I would check what is inside your savedJobs array. It seems that savedJobs would look like this:

savedJobs = ['Technician', 'Plumber'];

On the other hand, you are mapping and getting the value like so: {job.Key} & {job.Value}

Try:

<List>
    {savedJobs.map((job, i) => (
    <ListItem key={i} disablePadding>
    <ListItemButton>
      <ListItemText  primary={job} />
    </ListItemButton>
  </ListItem>
    ))}
</List>

ps: you should not use i as a key, but I let you figuring out what value you want there.

If you want to use {job.key} and {job.value}, you need to push an object like so:

  
  savedJobs.push({key: i, value: localStorage.getItem(keys[i])});

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