I'm facing a challenge with a component that requires an array parameter to display data, along with another array of objects (different type) that also needs to be passed to the component.
The prop type for my component is:
type ComponentType = {
label: string
children: ReactNode
}[]
The object type I have is:
type TemplatesType = {
title: string
description: string
}[]
I attempted to map this object array but encountered the error
Type '{ label: string; children: ReactNode; }[][]' is not assignable to type 'ComponentType[]'
:
const mapped: ComponentType[] = templates.map( ( {
title,
description
}) => [ {
label: 'Title',
children: <>{ title }</>
}, {
label: 'Description',
children: <>{ description }</>
} ] )
I also tried using multiple spreads within my object array which resolved the error, but led to incorrect data rendering order (now displaying:
title, title...., description, description...
instead of the desired rendering order: title, description, title, description....
):
const mapped: ComponentType[] = [
...templates.map(({ title }) => ({
label: 'Title',
children: <>{ title }</>
})),
...templates.map(({ description }) => ({
label: 'Description',
children: <>{ description }</>
})),
]
How can I resolve this error and ensure everything renders in the correct order?