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

207
Vistas
Is there anyway to pass state to getServerSideProps

I am new to next.js. I want to pass page state to getServerSideProps. Is it possible to do this?

const Discover = (props) => {
  const [page, setPage] = useState(1);
  const [discoverResults, setDiscoverResults] = useState(props.data.results);

  // console.log(discoverResults, page);

  return (
    <div>
      <Card items={discoverResults} render={(discoverResults) => <DiscoverCard results={discoverResults} />} />
    </div>
  );
};

export default Discover;

export async function getServerSideProps() {
  const movieData = await axios.get(`https://api.themoviedb.org/3/discover/movie?api_key=${process.env.NEXT_PUBLIC_MOVIE_DB_KEY}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=${page}&with_watch_monetization_types=flatrate`);

  return {
    props: {
      data: movieData.data,
    },
  };
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

the only way is changing your route with params and recive it in server side :

import { useRouter } from "next/router";

const Discover = (props) => {
  const { page } = props;
  const router = useRouter();

  const goToNextPage = () => {
    router.replace(`/your-page-pathname?page=${+page + 1}`);
  }

  return (
    <div>
      page is : {page}
      <button onClick={goToNextPage}>
        next page
      </button>
    </div>
  );
};

export default Discover;

export async function getServerSideProps(context) {
  const  { page } = context.query;
  return {
    props: {
      page: page || 0,
    },
  };
}

To read more on the topic, recommend reading: Refreshing Server-Side Props

about 4 years ago · Juan Pablo Isaza Denunciar

0

I recommend to use SWR for handling this kind of api calls

an example of this here: https://swr.vercel.app/examples/ssr

In this example, it can be seen that the api calls happens in the Server side and it is being cached in the Client side.

For handling the query from the urls. This can be done using the same methods as well following the examples from their documentation of SWR https://swr.vercel.app/docs/pagination#pagination

SWR will help alot of stuffs in the api state management. I really recommend to start learning it as soon as possible..

about 4 years ago · Juan Pablo Isaza Denunciar
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