Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextJs GetServerSideProps after update how can i call?

Below I am pulling all the data from the database. But let's say I deleted one piece of data. How can I retrieve the renewed data? How can I run it again?

export async function getServerSideProps() {
  const res = await fetch(`http://localhost:8000/api/getAllShipmentTypes`);
  const shipmentTypes = await res.json();

  return {
    props: { shipmentTypes } // will be passed to the page component as props
  };
}

like image 669
Bayoguzhan Avatar asked Nov 16 '25 09:11

Bayoguzhan


2 Answers

But let's say I deleted one piece of data. How can I retrieve the renewed data?

I think you will need to define what is the trigger for the deletion, I can think of these two.

  1. Another action user performs on a page.
  2. Some other system modifying the database that this client application shows.

For #1, To the action, say a button click you can use a router object to set the same route again which will run getServerSideProps again

When you request this page on client-side page transitions through next/link or next/router, Next.js sends an API request to the server, which runs getServerSideProps

For #2 - this would be handled by giving the user an option to refetch the data from the server again using a link or router component

like image 168
Ramakay Avatar answered Nov 18 '25 22:11

Ramakay


You can do something like:

import { useRouter } from 'next/router';

function SomePage(props) {

  const router = useRouter();

  // Call this function whenever you want to
  // refresh props!
  const refreshData = () => {
    router.replace(router.asPath);
  }

}
like image 23
Felipe Avatar answered Nov 18 '25 22:11

Felipe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!