How to pass props to {this.props.children}

后端 未结 26 3834
猫巷女王i
猫巷女王i 2020-11-21 23:42

I\'m trying to find the proper way to define some components which could be used in a generic way:


  
  

        
26条回答
  •  春和景丽
    2020-11-22 00:15

    Got inspired by all the answers above and this is what I have done. I am passing some props like some data, and some components.

    import React from "react";
    
    const Parent = ({ children }) => {
      const { setCheckoutData } = actions.shop;
      const { Input, FieldError } = libraries.theme.components.forms;
    
      const onSubmit = (data) => {
        setCheckoutData(data);
      };
    
      const childrenWithProps = React.Children.map(
        children,
        (child) =>
          React.cloneElement(child, {
            Input: Input,
            FieldError: FieldError,
            onSubmit: onSubmit,
          })
      );
    
      return <>{childrenWithProps};
    };
    
    

提交回复
热议问题