在我们的项目中,我们有大约 10 个使用 react-lottie 库并加载了可加载组件库的动画。其中一些动画在 Gatsby 开发环境中崩溃。我已经设法缩小了哪些失败的范围,并且有 2 个。
该组件是这样构建的:
@LottieComponent.tsx
import React, { CSSProperties } from 'react';
import Lottie from 'lottie-react';
import json from './lottieAnimation.json';
interface Props {
styles?: CSSProperties;
}
export default ({ styles }: Props) => (
<Lottie style={styles} animationData={json} />
);
然后我们使用代码拆分:
@index.tsx
import loadable from '@loadable/component';
const ExchangeInfographic = loadable(
() => import('./animationComponents/ExchangeGraphic')
);
export {
ExchangeInfographic
};
然后我们将组件导入到模块中,如下所示:
import { ExchangeInfographic } from '../../components/Infographic';
const ExampleComponent = () => {
const [animationWasRendered, setAnimationWasRendered] = useState(false);
return (
<ReactVisibilitySensor
onChange={(isVisible) => {
isVisible && setAnimationWasRendered(true);
}}
partialVisibility
>
<SectionCustomComponent>
<Container>
<Col>
{animationWasRendered ? <ExchangeInfographic /> : <></>}
</Col>
</Row>
</Container>
</Section>
</ReactVisibilitySensor>
);
};
export default ExampleComponent;
我在控制台中得到的错误是:
react-dom.development.js:23966 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `InnerLoadable`.
我检查了此错误消息的可能原因并找到了此线程: https ://github.com/facebook/react/issues/13445 和 代码拆分/react-loadable 问题
但看起来动态导入不会失败。令人困惑的部分是,为什么它只会在某些动画上崩溃而其他每次都可以正常工作。哦
你知道我错过了什么吗?
感谢您的任何建议!