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

432
Views
How to pass a function as props to React Children?

I want to pass this handle function to all children's of my Layout component. I know how to do it on custom components but on children I can't get it to work.

I tried the solution from here but I keep getting this warning:

enter image description here

Here is my code:


function Layout({ preview, children }) {
  
  const [hovered, setHovered] = useState(false);

  function toggleHover() {
    setHovered(!hovered);
  }

  const childrenWithProps = React.Children.map(children, (child) => {
    if (React.isValidElement(child)) {
      return React.cloneElement(child, { toggleHover });
    }
    return child;
  });

  return (
    <>
      <Mouse hovered={hovered} />

      <div>
        <Navbar toggleHover={toggleHover} />
        <main>{childrenWithProps}</main>
        <Footer toggleHover={toggleHover} />
      </div>
    </>
  );
}

export default Layout;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function Mouse({hovered}) {
  return null;
};

function Navbar({toggleHover}) {
  return <div onMouseEnter={toggleHover}>Hover on me</div>
};

function Footer({toggleHover}) {
  return <div onMouseEnter={toggleHover}>Hover on me</div>
};

function Child({toggleHover}) {
  return <div onMouseEnter={toggleHover}>I am child, hover on me</div>
};

function Layout({ preview, children }) {
  
  const [hovered, setHovered] = React.useState(false);

  function toggleHover() {
    console.log("hovered");
    setHovered(!hovered);
  }

  const childrenWithProps = React.Children.map(children, (child) => {
    if (React.isValidElement(child)) {
      return React.cloneElement(child, { toggleHover: toggleHover });
    }
    return child;
  });

  return (
    <div>
      <Mouse hovered={hovered} />

      <div>
        <Navbar toggleHover={toggleHover} />
        <main>{childrenWithProps}</main>
        <Footer toggleHover={toggleHover} />
      </div>
    </div>
  );
}

function Parent() {
  return (
    <div>
      <Layout>
        <Child />
        <Child />
        <Child />
      </Layout>
    </div>
  )
}

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Parent />,
  rootElement
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

<body>
    <div id="root"></div>
</body>

Problem is on function childrenWithProps, especially in React.cloneElement props argument. In your example you are using value toggleHover which essentially transforms into toggleHover: toggleHover.

To make it work you need to transform passed props into this: children_prop_name: toggleHover.

A more detailed example:

Function handling children properties:

const childrenWithProps = React.Children.map(children, (child) => {
  if (React.isValidElement(child)) {
    return React.cloneElement(child, { toggleProp: toggleHover });
  }
  return child;
});

A child component:

function Child({toggleProp}) {
  return <div onHover={toggleProp}>Click</div>
}
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!