-1

I have a simple component:

const Test = ({header}) => <View> <Header /> </View>`

and call it like so:

<Test header={<View> Test </View} />

but it fails saying:

JSX element type 'Header' does not have any construct or call signatures

How do I fix this? I need to render it like <Header /> so I can pass further props down to it

I don't want to render it like {header} this.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Red Baron
  • 7,181
  • 10
  • 39
  • 86

4 Answers4

0

If you want to pass props to your children have a look at the React API with the function cloneElement

You can foreach your children using React.Children

function Component({ children, ...props }) {
    const newChildren = React.Children.map(children, child => {
        return React.cloneElement(child, { ...child.props, ...props });
    });
   return (
       <div>
          {newChildren}
       </div>
);

}

alaric
  • 169
  • 6
-1

You can make it like this:

const Test = ({children}) => <View> {children} </View>

and use it like this:

<Test>
  <Header/>
</Test>

For more read the docs composition-vs-inheritance

Hassan Kandil
  • 1,612
  • 1
  • 13
  • 26
-1

you can try to do this

const Test = ({header})=>{
  
  return <View>
   {React.cloneElement(header, ObjectContainingPropsYouWantToPass)}
  </View>
}

source

Hadi KAR
  • 384
  • 3
  • 11
-1

Let me know if you are trying to achieve this

const Test = ({header: Header}) => <View> <Header /> </View>

Arunsridher
  • 43
  • 1
  • 5
  • Welcome. It is in general more useful to add this type of suggestion as a comment to the question instead of an answer (which it clearly isn't). That likely explains the downvotes you are receiving. – Ray Oei Mar 23 '21 at 15:15