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

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

    I think a render prop is the appropriate way to handle this scenario

    You let the Parent provide the necessary props used in child component, by refactoring the Parent code to look to something like this:

    const Parent = ({children}) => {
      const doSomething(value) => {}
    
      return children({ doSomething })
    }
    

    Then in the child Component you can access the function provided by the parent this way:

    class Child extends React {
    
      onClick() => { this.props.doSomething }
    
      render() { 
        return (
    ); } }

    Now the fianl stucture will look like this:

    
      {(doSomething) =>
       (
         
         
        
       )}
    
    

提交回复
热议问题