React.lazy() 与 Typescript

Jil*_*dMe 3 typescript reactjs

我收到错误:

类型“Element”中缺少属性“default”,但类型“{default: ComponentType;”中需要属性“default” }'.ts(2322)

   {React.lazy(() => import(`../../i18n/locales/${this.props.lang}`).then(o => (
              <CustomSelects
                options={o.option}
                formattedMessageId="createroom_genre"
                value={this.props.genre}
                handleValueChange={this.handleGenreChange}
              />
            ))) }
Run Code Online (Sandbox Code Playgroud)

我阅读了Using React.lazy with TypeScript,但是,我不知道在我的情况下如何执行此操作。

如果您需要更多信息,请告诉我。谢谢。

Est*_*ask 5

组件不应在使用它们的地方就地声明,这可能会破坏优化并可能导致无限循环。

React.lazy旨在与dynamic一起使用import,回调函数应该返回一个模块的promise,即{ default: Component }对象,但它当前返回一个React元素的promise。

React.lazy并不是要参数化的,它不接受 props 并且this.props.lang不能在那里工作。它应该用另一个组件包装来接收道具:

const LocalCustomSelects = React.memo(({ lang }) => {
  const Lazy = React.lazy(async () => {
    const o = await import(`../../i18n/locales/${lang}`);
    return { default: props => <CustomSelects ... /> };
  });

  return <Lazy/>;
});
Run Code Online (Sandbox Code Playgroud)