admin管理员组文章数量:1399963
Issue:
I'm trying to use @loadable/component
for dynamic imports in my React project, but I get the following error when running:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! Found: [email protected]
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.3.0 || ^17.0.0 || ^18.0.0" from @loadable/[email protected]
It seems @loadable/component
does not yet support React 19.
Example:
In my component, I currently use:
import loadable from '@loadable/component';
const EmojiPickerComponent = loadable(() => import('./EmojiPicker'), {
fallback: <p id="loading">Loading...</p>
});
This no longer works with React 19.
Question:
What is the recommended way to achieve the same dynamic import functionality with fallback in React 19?
Is there any official or community-recommended alternative to@loadable/component
for React 19?
Example use case:
const EmojiPickerComponent = ??? // What should I use here?
{showEmojiContainer && (
<EmojiPickerComponent
onEmojiClick={(event, emojiObject) => {
setMessage((prev) => `${prev} ${emojiObject.emoji}`);
}}
/>
)}
Environment:
- React: 19.0.0
- Node.js: 22.14.0
- npm: 10.9.2
Any suggestions or guidance would be appreciated!
Issue:
I'm trying to use @loadable/component
for dynamic imports in my React project, but I get the following error when running:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! Found: [email protected]
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.3.0 || ^17.0.0 || ^18.0.0" from @loadable/[email protected]
It seems @loadable/component
does not yet support React 19.
Example:
In my component, I currently use:
import loadable from '@loadable/component';
const EmojiPickerComponent = loadable(() => import('./EmojiPicker'), {
fallback: <p id="loading">Loading...</p>
});
This no longer works with React 19.
Question:
What is the recommended way to achieve the same dynamic import functionality with fallback in React 19?
Is there any official or community-recommended alternative to@loadable/component
for React 19?
Example use case:
const EmojiPickerComponent = ??? // What should I use here?
{showEmojiContainer && (
<EmojiPickerComponent
onEmojiClick={(event, emojiObject) => {
setMessage((prev) => `${prev} ${emojiObject.emoji}`);
}}
/>
)}
Environment:
- React: 19.0.0
- Node.js: 22.14.0
- npm: 10.9.2
Any suggestions or guidance would be appreciated!
Share Improve this question asked Mar 24 at 16:55 Arman KhanArman Khan 448 bronze badges1 Answer
Reset to default 0Since @loadable/component does not currently support React 19, you can use React's built-in React.lazy and Suspense for dynamic imports. React.lazy is natively supported and works well with React 19.
Alternative Solution using React.lazy:
import React, { lazy, Suspense } from 'react';
const EmojiPickerComponent = lazy(() => import('./EmojiPicker'));
{showEmojiContainer && (
<Suspense fallback={<p id="loading">Loading...</p>}>
<EmojiPickerComponent
onEmojiClick={(event, emojiObject) => {
setMessage((prev) => `${prev} ${emojiObject.emoji}`);
}}
/>
</Suspense>
)}
Why React.lazy?
- Native to React – No external dependencies needed.
- Works with React 19 – Fully compatible.
- Same functionality – Provides a fallback while the component loads.
If you need SSR support (which React.lazy lacks), alternatives like Next.js dynamic imports (next/dynamic) may be useful.
本文标签:
版权声明:本文标题:javascript - How to dynamically import components with fallback in React 19 (alternative to @loadablecomponent)? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744238843a2596689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论