Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
NextJS change data in API from component

I've got a problem with updating data from API in NextJS. I want to increment the value by clicking on the button and then update the API. My local JSON file is in at http:localhost/api/data

data.js:

export default function handler(req, res) {
  res.status(200).json({ value: 0 })
}

I want to update this value from Home component

index.js:

import { useState } from 'react'

export default function Home({data) {
  const [currentData, setCurrentData] = useState(data)
  return (
    <div>
      {data.value}
      <button onClick={(e) => setCurrentData(currentData.value + 1)}>+</button>
    </div>
  )
}

export async function getServerSideProps() {
  const res = await fetch('http://localhost:3000/api/data')
  const data = await res.json()
  return {
    props: { data },
  }
}

Is there any way to do it? If something is unclear feel free to ask :)

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda