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

149
Visualizações
How to communicate back to a page it should display a notification once?

I have a registration page that will report back to the index when the operation was successful. Thet communication, as of right now, is happening through query params.

const Foo: React.FC = () => {
    const router = useRouter();
    const [useModal, setUseModal] = useState(false);

    const closeModal = () => {
        setUseModal(false);
    };

    const generateModalContent = (queryContent: ParsedUrlQuery) => {
        if (Object.keys(queryContent).length !== 0) {
            return {
                //key-values from query
            };
        }
    };
    const modalContent = generateModalContent(router.query);

    if (Object.keys(modalContent!).length !== 0 && useModal === false) {
        setUseModal(true);
    }

    return (
        <>
            //unrelated stuff
            {useModal ? <ModalNotification {...modalContent} /> : null}
        </>
    );
};

Problem is that every time closeModal is called to close the modal, it is shown again since, upon re-render, the query is still there and the cycle will repeat. I'm looking for a clean way to display the data sent by query once, and upon the user closing the modal it doesn't show up again.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To avoid calling that part of your code in every re-render you must wrap it in a useEffect with the correct dependencies like so:

React.useEffect(() => {
    const modalContent = generateModalContent(router.query);

    if (Object.keys(modalContent!).length !== 0 && useModal === false) {
        setUseModal(true);
    }
}, [router]);

That way it will only execute when router changes and not in an infinite cycle.

You can read more about it in the React docs.

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