1

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;

FASP
  • 45
  • 1
  • 2
  • 7

1 Answers1

1

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>
}
  • I tried your solution but I get `toggleProp: undefined` and the same warning. @michal zagrodzki – FASP Jan 17 '22 at 19:27
  • @FASP Added working code snippet. Please have a look on it again. – michal zagrodzki Jan 18 '22 at 08:01
  • 1
    It worked! But I had to put all content of my Index page inside a component to then use that component inside Layout. Thanks! @michal zagrodzki – FASP Jan 18 '22 at 14:42