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

后端 未结 26 3428
猫巷女王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:35

    The best way, which allows you to make property transfer is children like a function pattern https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9

    Code snippet: https://stackblitz.com/edit/react-fcmubc

    Example:

    const Parent = ({ children }) => {
        const somePropsHere = {
          style: {
            color: "red"
          }
          // any other props here...
        }
        return children(somePropsHere)
    }
    
    const ChildComponent = props => 

    Hello world!

    const App = () => { return ( {props => ( Bla-bla-bla )} ) }

提交回复
热议问题