I’m using React 18 with Vite and Ionic and I’m trying to lazy-load the IonDatetime component:
import { IonDatetime } from '@ionic/react';
const ControllerDatetime: React.FC<ControllerDatetimeProps> = ({
control,
label,
name,
clearErrors,
disabled,
}: ControllerDatetimeProps) => {
return (
<Controller
control={control}
name={name}
rules={{ required: true }}
render={({
field: { onChange, onBlur, value },
fieldState: { invalid, isTouched },
}) => (
<IonDatetime>
{label}
</IonDatetime>
)}
/>
);
}
I can’t figure out how to use lazy-loading here though.
I tried
const IonDatetime = React.lazy(() => import { IonDatetime } from '@ionic/react')
but it results in a syntax error.
I’m using lazy loading successfully to get default exports of my own components, but how do I lazy-load a specific component from a third-party library?
2
Answers
You can avoid the intermediate module by doing this:
It’s annoying to type, so I’ve personally started to use react-lazily instead, which will reduce the above to