1

I have a list of options in a React component which I am mapping through to make some UI. The component is being used in QwikJS, which supports React components and uses Vite. However, when I add an icon value in my list of options Vite breaks during the build.

import { IconHome, IconMail, IconPencilPlus } from '@tabler/icons'

interface OptionProps {
    id: number
    text: string
    icon: React.ReactElement
    url?: string
    action?: () => void
}

export const Command = qwikify$(() => {
    ...
    const options: OptionProps[] = [
        {
            id: 1,
            text: 'Home',
            icon: <IconHome className='h-5 w-5' stroke={1} />,
            url: '/',
            action: undefined,
        },
        {
            id: 2,
            text: 'Join the waitlist',
            icon: <IconPencilPlus className='h-5 w-5' stroke={1} />,
            url: undefined,
            action: () => {
                setShowWaitlist(true)
            },
        },
     ]
     ...
}

I can't work out how to define the icon. Vite keeps giving me the error:

[vite] Internal server error: Cannot read properties of undefined (reading 'IconHome')

I've used the same code in Next (this is Qwik). Can anyone see what I'm doing wrong? - would like to render the icon when I map through the options, ie:

{options.map((option, i) => (
    <div>
        <div>{option.icon}</div>
        <div>{option.text}</div>
    <div>
))}
Ray Purchase
  • 681
  • 1
  • 11
  • 37

1 Answers1

0

import { IconHome, IconMail, IconPencilPlus } from '@tabler/icons'

interface OptionProps {
    id: number
    text: string
    icon: () => React.ReactElement
    url?: string
    action?: () => void
}

export const Command = qwikify$(() => {
    ...
    const options: OptionProps[] = [
        {
            id: 1,
            text: 'Home',
            icon: () => <IconHome className='h-5 w-5' stroke={1} />,
            url: '/',
            action: undefined,
        },
        {
            id: 2,
            text: 'Join the waitlist',
            icon: () => <IconPencilPlus className='h-5 w-5' stroke={1} />,
            url: undefined,
            action: () => {
                setShowWaitlist(true)
            },
        },
     ]
     ...
}


{options.map((option, i) => (
    <div>
        <div>{option.icon()}</div>
        <div>{option.text}</div>
    <div>
))}

Harsh Mangalam
  • 1,116
  • 1
  • 10
  • 19
  • I don't know why changing a `React.Component` into a function inside an object could be the solution. Your approach doesn't work for me. But this approach https://stackoverflow.com/a/71945129/8339172 by adding configuration for `vite` to process `jsx` inside a `js` file works for me. – Jabal Logian Jul 24 '23 at 09:07