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

162
Visualizações
Passing the callback function

I have a Modelmenu that is nested in the parent component, it implements the function of opening a modal window by click. Also in this parent component there is a child component to which you need to bind the function of opening a modal window by one more element. I can do this if all the logic is in the parent component, but this is a bad practice, since I will have to duplicate the code on each page in this way. How can I do this? I'm sorry, I'm quite new, I can't understand the callback.

Parent:

 const Home: NextPage = () => {
    const handleCallback = (handleOpen: any) => {

    }
    return (
        <>
            <ModalMenu parentCallback={handleCallback}/>
            <Slider handleCallback={handleCallback}/>
        </>
    )
}

Modal:

export const ModalMenu: FC = (props) => {

    const [play, setPlay] = useState<boolean>(false)

    const handleOpen = () => {
        props.parentCallback(setPlay(!play))
    };
    const handleClose = () => {
        setPlay(false)
        setPlay(!play)
    };

    return
}

Child:

export const Slider: FC= (props) => {

    return (
        <>
            <Image nClick={props.handleCallback}/>
        </>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're looking to pass down values and/or functions from a parent to two or more children, I think it's better to just have the values and functions in the parent

Parent :

const Home: NextPage = () => {
    const [play, setPlay] = useState<boolean>(false)

  const handleOpen = () => {
        setPlay(prev => !prev)
    };
   
    const handleClose = () => {
        setPlay(false)
        setPlay(!play)
    };

    return (
        <>
            <ModalMenu handleOpen={handleOpen} handleClose={handleClose} play={play}/>

            <MainSlider  <ModalMenu handleOpen={handleOpen} handleClose={handleClose} play={play}/>
        </>
    )
}

if you want to pass an interface to the props in the children for typescript your interface will look something like this

interface iProps {
 play : boolean;
 handleOpen : () => void;
 handleClose : () => void;
}
export const ModalMenu: FC = (props):iProps => {
// you can easily access all you want
const {handleClose, handleOpen, play} = props

    return
}
export const Slider: FC= (props): iProps => {
const {handleClose, handleOpen, play} = props
    return (
        <>
            <Image onClick={handleOpen}/>
        </>
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