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

147
Visualizações
How to save new state in db.json?

I have an array in my db.json. And I can add a new object. But every time I reload the page, the data is reset to the default state. How can I store data in such a way that it is written to db.json and displayed on reload?

My db.json:

{"item": [
    {
        "text": "Пошел в свой первый класс",
        "id": 0,
        "data": {
            "year": 2012,
            "day": 25,
            "month": 1
        }
    },
    {
        "text": "Поехал на чемпионат по бейсболу",
        "id": 1,
        "data": {
            "year": 2018,
            "day": 14,
            "month": 3
        }
    },
    {
        "text": "Поступил в институт",
        "id": 2,
        "data": {
            "year": 2007,
            "day": 12,
            "month": 4
        }
    }
]}

My state:

const [value, setValue] = useState('');
const [valueYear, setValueYear] = useState('');
const [valueDate, setValueDate] = useState('');
const [valueMonth, setValueMonth] = useState('');

const [table, setTable] = useState([]);

useEffect(() => {
axios.get("http://localhost:3004/item").then(({ data }) => {
  setTable(data);
})}, [])

My add function:

 const addTask = (value, valueYear, valueDate, valueMonth) => {
if (value) {
  const newItem = {
    id: table.length,
    text: value,
    data: {
      year: valueYear,
      day: valueDate,
      month: valueMonth,
    },
  }
  setTable([...table, newItem]);
}}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To whrite a file in Javascript you need to use NodeJS. In this case I advice you use localStorage. When you set new state of table put the follow code:

  setTable([...table, newItem]);
  localStorage.setItem('table', [...table, newItem]);

And capture it on initial state, like this:

const [table, setTable] = useState(localStorage.getItem('table') || []);
about 4 years ago · Juan Pablo Isaza Relatório

0

 const addTask = (value, valueYear, valueDate, valueMonth) => {
if (value) {
  const newItem = {
    id: table.length,
    text: value,
    data: {
      year: valueYear,
      day: valueDate,
      month: valueMonth,
    },
  }
  axios.post('http://localhost:3004/item', newItem).then(() => {
    setTable([...table, newItem]);
  })
}}
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