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

71
Visualizações
Data pushed in a json object erases when the page is refreshed

I am storing a data in a json object. The data is then displayed in a table. But when I refresh the page, all the data is lost. How can I retain the data in the object once it is pushed in the object.

product.json file is an empty json object because I need data to be pushed in it.

<Button
                          color="success"
                          type="button"
                          className="waves-effect waves-light me-1"
                          onClick={() => {
                            products.push({
                              eventID: products.length + 1,
                              title: this.state.title,
                              startDate: this.state.startDate,
                              endDate: this.state.endDate,
                              time: this.state.time,
                              address: this.state.address,
                              link: this.state.URL,
                              details: this.state.details,
                              uploadedImage: "Image not available",
                            });
                            console.log(products);
                            this.setState({ addNew: false });
                          }}
                        >

Pushing the data in this way. How can I keep the data stored?

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

0

You need to store added data somewhere, like server or local storage depends on type of data etc.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can write JSON into local storage and just use JSON.stringify (non-jQuery) to serialize a JavaScript object. You cannot write to any file using JavaScript alone. Just cookies or local (or session) storage.

const products = [
    {
        name: 'Dhayalan',
        score: 100
    }
];

localStorage.setItem('productsStorage', JSON.stringify(products));

And to retrieve the object later, such as on page refresh or browser close/open...

const storedData = JSON.parse(localStorage.getItem('productsStorage'));

We might do something like this also -

<script>
    let products = [];
    function storeData(data){
        products = retriveData();
        products.push(data)
        localStorage.setItem('productsStorage', JSON.stringify(storedData));
    }

    function retriveData(){
        try{
            return JSON.parse(localStorage.getItem('productsStorage'));
        } catch(e) {
            return [];
        }
    }
</script>

<Button
    color="success"
    type="button"
    className="waves-effect waves-light me-1"
    onClick={() => {
    this.storeData({
        eventID: products.length + 1,
        title: this.state.title,
        startDate: this.state.startDate,
        endDate: this.state.endDate,
        time: this.state.time,
        address: this.state.address,
        link: this.state.URL,
        details: this.state.details,
        uploadedImage: "Image not available",
    })
    console.log(products);
    this.setState({ addNew: false });
    }}
>
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