【发布时间】:2020-11-23 05:01:45
【问题描述】:
我有一个暴露 TypeScript 模块的 monorepo,它被 React TypeScript 项目消耗和使用。
当模块将任意 React 元素插入到虚拟 DOM 时 - 一切都按预期工作,包括当我尝试使用 React Router 时(最初是有问题的,但我能够修复它)。
但是,当我尝试通过 FormattedMessage 使用 react-intl 时,出现错误:
Error: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.
当我在控制台日志中看到这一点时,这尤其令人讨厌:
The above error occurred in the <Context.Consumer> component:
in FormattedMessage
in h2
in div
in Loading (at App.tsx:11)
in IntlProvider (at App.tsx:8)
in App (at src/index.tsx:9)
in StrictMode (at src/index.tsx:8)
(注意 IntlProvider 包装 Loading - 这是使用 FormattedMessage 的元素,但找不到 IntlProvider)。
我想这在某种程度上与版本控制有关,或者有 2 个 React / React DOM / IntlProvider 实例,但我不知道如何解决这个问题,我花了很多时间尝试我能想到的一切。
对于它的价值,这是我使用的:
- TypeScript - 适用于模块和项目
- Webpack 打包模块,我将 React、ReactDOM 和 react-intl 声明为
externals并将它们添加为peerDependencies而不是直接依赖项 -
create-react-app代表项目
我能够创建一个最小的 repro 存储库,这里是如何重现我的问题:
<cd somewhere>
git clone https://github.com/chakaz/repro-repo .
cd repro-lib
npm install
npm run build:dev
cd ../project
npm install
npm run start
有人知道吗?提前致谢!
【问题讨论】:
标签: reactjs typescript webpack tsconfig react-intl