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

161
Vistas
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 Respuestas
Responde la pregunta

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