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

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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