我希望在我的React应用程序中实现延迟加载,但大部分文件都是JSON格式。当视口宽度小于768px时,其中的一些文件会被加载,但在桌面应用程序中不需要时,我不想加载它们。是否有办法使用React.lazy()加载JSON文件?
我的JSON文件是使用Adobe After Effect扩展程序“Bodymovin”生成的,然后我像这样导入该文件:
import * as background from './background.json';
import * as backgroundLarge from './background-large.json';
import * as backgroundMedium from './background-medium.json';
import * as backgroundMobile from './background-mobile.json';
import * as backgroundSmallMobile from './background-smallMobile.json';
import * as tree from './tree.json';
import * as treeLarge from './tree-large.json';
import * as treeMedium from './tree-medium.json';
import * as treeMobile from './tree-mobile.json';
import * as treeSmallMobile from './tree-smallMobile.json';
我尝试像加载其他组件一样使用默认导出方式加载它,但这并不起作用。
const treeMedium = lazy(() => import('./tree-medium.json'));
通常情况下,我导入JSON后会像这样稍后使用它:
backgroundOptions: {
loop: true,
autoplay: true,
animationData: background.default,
},
将此对象传递给
react-lottie
中的Lottie
组件。我能将它转换为懒加载吗?还是我的想法有误?
use
钩子:const data = use(import('./data.json'))
- undefined