[英]React Native monorepo: TS2786: 'View' cannot be used as a JSX component
I have a monorepo with the following structure:我有一个具有以下结构的monorepo:
repo/
├─ node_modules/
├─ package.json
├─ packages/
│ ├─ design-system/
│ │ ├─ package.json // dependencies installed in repo/node_modules
├─ apps/
│ ├─ my-app/
│ │ ├─ node_modules/
│ │ ├─ package.json // dependencies installed in my-app/node_modules
repo/packages contains shared packages to be used by various apps in repo/apps. repo/packages 包含 repo/apps 中各种应用程序使用的共享包。 Shared packages have their dependencies in repo/node_modules, managed via Yarn Workspaces in the top level package.json:
共享包在 repo/node_modules 中有它们的依赖项,通过顶级 package.json 中的 Yarn Workspaces 进行管理:
"workspaces": [
"packages/*"
]
repo/apps contains React Native apps. repo/apps 包含 React Native 应用程序。 Apps in repo/apps have their own local node_modules and are NOT Yarn Workspaces (I decided against that because managing React Native dependencies with Yarn Workspaces was too cumbersome and flaky).
repo/apps 中的应用程序有自己的本地 node_modules 并且不是 Yarn Workspaces(我决定反对,因为使用 Yarn Workspaces 管理 React Native 依赖项过于繁琐和易碎)。
My current workflow is to run yarn install
in the top level dir, then cd
into repo/apps/my-app and run yarn install
to install the local node_modules.我当前的工作流程是在顶级目录中运行
yarn install
,然后cd
进入 repo/apps/my-app 并运行yarn install
来安装本地 node_modules。
This has been working well, until now...这一直运作良好,直到现在......
I have a design-system
shared package with the following deps:我有一个包含以下部门的
design-system
共享包:
"devDependencies": {
"@types/react": "17.0.2",
"@types/react-native": "^0.67.1"
},
"peerDependencies": {
"react": "17.0.2",
"react-dom": "16.3.1",
"react-native": "0.67.3"
}
my-app/package.json has many many dependencies, but I think the relevant one here is: my-app/package.json 有很多依赖项,但我认为这里相关的是:
"devDependencies": {
"@types/react-native": "^0.67.1"
}
(note how "@types/react": "17.0.2" is NOT a dependency of my-app. I have tried adding it, did not change anything). (注意 "@types/react": "17.0.2" 不是 my-app 的依赖项。我尝试添加它,但没有改变任何东西)。
When I import it in a component in my-app
, I get the following error:当我将其导入
my-app
的组件中时,出现以下错误:
import { Button } from 'design-system'
import { View } from 'react-native'
const AppComponent = () => (
<View> // error TS2786
<Button/>
</View>
)
TS2786: 'View' cannot be used as a JSX component.
Its instance type 'View' is not a valid JSX element.
The types returned by 'render()' are incompatible between these types.
Type 'React.ReactNode' is not assignable to type 'import("/Users/sam/repo/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode'.
Type '{}' is not assignable to type 'ReactNode'.
What I understand from this error is that there is a conflict between the ReactNode
type in the top level node_modules vs the one in the app node_modules.我从这个错误中了解到,顶级 node_modules 中的
ReactNode
类型与应用程序 node_modules 中的类型之间存在冲突。
My question is how and where do i fix this?我的问题是我如何以及在哪里解决这个问题?
The answer was right here .答案就在这里。
After inspecting my top level yarn.lock, I found that React 18 was indeed making its way into it.在检查了我的顶级 yarn.lock 之后,我发现 React 18 确实正在进入其中。
"@types/react@*":
version "18.0.9"
I had to add resolutions in the top level package.json:我必须在顶级 package.json 中添加分辨率:
// repo/package.json
"resolutions": {
"@types/react": "17.0.2",
"@types/react-dom": "17.0.2"
},
Which then changed the lock file to然后将锁定文件更改为
"@types/react@*", "@types/react@17.0.2":
version "17.0.2"
And the errors went away 🥳 🕊错误消失了🥳🕊
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.