Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!