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

154
Visualizações
Only trigger UseEffect() in Material UI Dialog when it is in opened

I have a parent component that contains a Material UI Dialog (child). Now this dialog's purpose is to fetch and display data from the REST API.

Currently this works by implementing UseEffect() in the Dialog component. However as soon as the parent component is mounted, the UseEffect() inside child component will kick in. The problem is I will be missing information that are supposed to be passed from the parent to the child itself (because both of this component are mounted at the same time).

Now what got me thinking is, I want that this dialog only be mounted when the button to show it is being clicked. Is that possible?

This is the snippet:

Parent.js

    const Parent = React.forwardRef((props, ref) => {

            const [openChildDialog, setOpenChildDialog] = useState(false)
    
        useEffect(() => {
// function here. This also going to set value in a state that will be used for the child component
        })

        const handleOpenChildDialog = () => {
            setOpenChildDialog(true)
        }
        const handleCloseChildDialog = () => {
            setOpenChildDialog(false)
        }
        
            return (
                <Page>
                    <PageTitle>
                        Parent Component Test
                    </PageTitle>

                // Dialog will only be mounted when this button is clicked.
                <Button onClick={handleOpenChildDialog}> Open the child dialog! </Button>
    
                    <ChildDialog
                        open={openChildDialog}
                        onClose={handleCloseChildDialog}
                    />
                </Page>
            )
        })

If what I am asking is not possible, then I am open to alternative as long as the UseEffect() inside the child dialog component is not immediately executed when the parent is mounted.

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

0

UseEffect() behaves so that it is executed when the component is mounted, updated and unmounted. However the solution I see here it is using a conditional to render your child component when your openChildDialog change to true

{ openChildDialog &&
    <ChildDialog
        open={handleOpenChildDialog}
        onClose={handleCloseChildDialog}
    />

}

I leave you this incredible guide so you can see in depth how to use this hook: https://overreacted.io/a-complete-guide-to-useeffect/

about 4 years ago · Juan Pablo Isaza Relatório

0

to only render the ChildDialog component when it's open, simply wrap it in a conditional:

{ openChildDialog && (
  <ChildDialog
    open={openChildDialog}
    onClose={handleCloseChildDialog}
  />
)}

in terms of your useEffect - you can include an array as the 2nd parameter of a useEffect, and then the funciton will only run when anything in the array changes, for example:

useEffect(() => {
  // this will run whenever any state or prop changes, as you haven't supplied a second parameter
})

useEffect(() => {
  // this will now only run when openChildDialog changes
  // you can easily put a check in here to see if openChildDialog is true to only run on open
}, [openChildDialog])

useEffect(() => {
  // an empty array means this will only run when the component is first mounted
}, [])

so to answer your useEffect-inside-child running error, you could do something like:

useEffect(() => {
 if (open) {
   // do stuff only when the open prop changes to true here
 }
}, [open])
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