Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!