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

121
Visualizações
How to store specific API data in local storage and show those data to another page in ReactJs?

I have a page called "Content" where I fetch all the data from an API and put only "name" and "bio" in a cart, in each cart I have a button called "Favourite". When the user clicks the button only cart data (in my case it's "name" and "bio") will store in LocalStorage.

So my first question is how I can store those specific data in local storage?

And in the "Fav-Content" page I want to fetch the local storage data that the user stored by clicking the "Favourite" button.

So my second question is how I can fetch and show those local storage data on another page?

Here is my approach:

import React, { useState, useEffect } from 'react';
import Card from 'react-bootstrap/Card';

const Posts = ({ posts, loading }) => {
    const [items, setItems] = useState([]);

    const click = () => {
        localStorage.setItem('items', JSON.stringify(items));
    };

    if (loading) {
        return <h2>Loading...</h2>;
    }

    return (
        <div className="fav-content">
            <ul className="card">
                {posts.map((item, index) => {
                    return (
                        <li key={index}>
                            <Card style={{ width: '18rem' }}>
                                <button onClick={click}>Add Favt</button>
                                <Card.Body>
                                    <Card.Title>Name: {item.name}</Card.Title>
                                    <Card.Text>Bio: {item.bio}</Card.Text>
                                </Card.Body>
                            </Card>
                        </li>
                    );
                })}
            </ul>
        </div>
    );
};

export default Posts;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I changed your code fragment and added get and set item functions to local storage.

import React, { useState, useEffect } from 'react';
import Card from 'react-bootstrap/Card';

const Posts = ({ posts, loading }) => {
// I dont think you need this state since yo uare not using it in this component
const [items, setItems] = useState([]);

const click = (name) => {
    const item = posts.find((post) => post.name === name);
    const localStorageItems = JSON.parse(localStorage.getItem('items')) || [].
    localStorage.setItem('items', JSON.stringify([...localStorageItems, item]))
};

if (loading) {
    return <h2>Loading...</h2>;
}

return (
    <div className="fav-content">
        <ul className="card">
            {posts.map((item, index) => {
                return (
                    <li key={index}>
                        <Card style={{ width: '18rem' }}>
                            <button onClick={() => click(item.name)}>Add Favt</button>
                            <Card.Body>
                                <Card.Title>Name: {item.name}</Card.Title>
                                <Card.Text>Bio: {item.bio}</Card.Text>
                            </Card.Body>
                        </Card>
                    </li>
                );
            })}
        </ul>
    </div>
);
};

export default Posts;

If you want to use items in annother page you should do localStorage.getItem('items') and parse it.

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