Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Drawer.Navigator' cannot be used as a JSX component with TypeScript #10507

Closed
3 of 11 tasks
challengy1 opened this issue Apr 13, 2022 · 39 comments
Closed
3 of 11 tasks

'Drawer.Navigator' cannot be used as a JSX component with TypeScript #10507

challengy1 opened this issue Apr 13, 2022 · 39 comments

Comments

@challengy1
Copy link

Current behavior

In the code as follows, I encountered a TypeScript Error message saying 'Drawer.Navigator' cannot be used as a JSX component.

import { View, Text } from "react-native";
import { createDrawerNavigator } from "@react-navigation/drawer";

type StackParamList = {
  Home: undefined;
};

const Drawer = createDrawerNavigator<StackParamList>();

const HomeScreen = () => {
  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
      <Text>HOME PAGE</Text>
    </View>
  );
};

export default function App() {
  return (
      <Drawer.Navigator initialRouteName="Home">
        <Drawer.Screen name="Home" component={HomeScreen} options={{ title: 'HOME '}} />
      </Drawer.Navigator>
  );
}

I encountered this issue with Bottom-tabs as well. With Native-Stack, however, No Typescript error was reported.

Expected behavior

I expect this TypeScript error message is resolved.

Reproduction

I attached the source code in the Current behavior. Nothing more.

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • @react-navigation/native-stack

Environment

"@react-navigation/drawer": "^6.4.1",
"@react-navigation/native": "^6.0.10",
"expo": "~44.0.0",
"expo-status-bar": "~1.2.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-gesture-handler": "~2.1.0",
"react-native-reanimated": "~2.3.1",
"react-native-safe-area-context": "3.3.2",
"react-native-screens": "~3.10.1",
"react-native-web": "0.17.1"

"@babel/core": "^7.12.9",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"typescript": "~4.3.5"

The version of the expo-cli was 5.3.1, the latest one.
I made this project with expo init <project_name> with TypeScript support. (Simple configuration).

I have added a plugin in babel.config.js as follows:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin'],
  };
};
@github-actions
Copy link

Hey! Thanks for opening the issue. The issue doesn't seem to contain a link to a repro (a snack.expo.dev link, a www.typescriptlang.org/play link or link to a GitHub repo under your username).

Can you provide a minimal repro which demonstrates the issue? A repro will help us debug the issue faster. Please try to keep the repro as small as possible and make sure that we can run it without additional setup.

@github-actions
Copy link

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs
  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

@challengy1
Copy link
Author

I haven't installed following packages since these packages are nothing to do with this case.
I don't provide a snack because this case is very simple and easy to reproduce.

I hope you understand this condition.

BR,

@satya164
Copy link
Member

I don't provide a snack because this case is very simple and easy to reproduce

If it's that simple to reproduce then you can take a few seconds to copy/paste it on snack and repro the issue. Regardless even if it looks simple to you, doesn't mean it'll actually be simple to reproduce.

Anyway, you haven't provided an error message either but from what I have seen, I can guess that this issue caused due to multiple versions of @types/react, so you have to identify if you have multiple versions, and get rid of duplicate versions:

  1. Run yarn why @types/react for Yarn and npm ls @types/react to check if there are multiple versions installed and why they are being installed
  2. If multiple version is from react-native and your devDependencies, the easy fix is to remove it from devDependencies
  3. If they are installed due to multiple packages depending on @types/react, you need to use Yarn resolutions or npm overrides

All of this is highly specific to your project structure and not-straightforward to repro. If you still have issues after following the above steps, please open a new issue with a repro and full error-message.

@github-actions
Copy link

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

@challengy1
Copy link
Author

Hi satya164, Thank you for our swift reply. In short the issue was resolved.
As you pointed out, there were 2 @types/react as follows:

>B> yarn why @types/react
yarn why v1.22.17
warning ../../package.json: No license field
[1/4] 🤔  Why do we have the module "@types/react"...?
[2/4] 🚚  Initialising dependency graph...
[3/4] 🔍  Finding dependency...
[4/4] 🚡  Calculating file sizes...
=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info This module exists because it's specified in "devDependencies".
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
=> Found "@types/react-native#@types/react@18.0.3"
info This module exists because "@types#react-native" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3

As soon as I remove the one with version 17, the issue was resolved.

Thank you so much again.

BR,

@awesome-jeremy
Copy link

awesome-jeremy commented Apr 15, 2022

By adding
"resolutions": { "@types/react": "~17.0.21" },
in package.json, and run 'yarn install' again can solve the issue.

The reason why there are both @type/react 17 and 18 versions is "react-native" is pulling the latest "@types/react" which is 18 in this case.

you can find it by checking yarn.lock file
`
"@types/react-native@~0.64.12":
version "0.64.24"
dependencies:
"@types/react" "*"

"@types/react@":
version "18.0.5"
dependencies:
"@types/prop-types" "
"
"@types/scheduler" "*"
csstype "^3.0.2"
`

@challengy1
Copy link
Author

Hi, Captain-Ray,

Thank you for your advice. Yes, as you point out the root cause of the issue is the major bump of @types/react. I have resolved the issue by adding resolutions field in package.json. Since then, it works fine. Not only React Navigation, many packages are influenced with changes in @types/react as mentioned in as follows:
Bug: React 18 types broken since the type release a view hours ago #24304.
Thank you again have a nice day.

BR,

@orome
Copy link

orome commented Apr 19, 2022

I can't get this to work. I've got

  "devDependencies": { "@types/react": "~17.0.21" }

and have tried

  "resolutions": { "@types/react": "~18.0.5" }

but no matter what I do (clear caches, run yarn again and again), I get the TypeScript/JSX error (in my IDE). This goes away if I put ~18.0.5in devDependencies, but then as soon as I expo start I'm forced to downgrade again with

It looks like you're trying to use TypeScript but don't have the required dependencies installed. Would you like to install @types/react? … no
Please install @types/react by running:

yarn add --dev @types/react@~17.0.21

If you're not using TypeScript, please remove the TypeScript files from your project and delete the tsconfig.json.

which then results in the TypeScript/JSX error again.

It appears one can't have it both ways: one has to chose between either

  • successfully running expo (with ~17.0.21) but getting a TypeScript error about JSX, or
  • avoiding the TypeScript/JSX error (with ~18.0.5) but failing to run expo(without downgrading) .

Is there any way around this dilemma?

@raajnadar
Copy link
Member

@orome you need to use, as per your dev dependence

"resolutions": {
    "@types/react": "17.0.43"
},

@challengy1
Copy link
Author

@orome,

Before using resolutions, I would recommend to confirm the version number of your @types/react by typing

yarn why @types/react

and you will get as follows:

=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info This module exists because it's specified in "devDependencies".
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
=> Found "@types/react-native#@types/react@18.0.3"
info This module exists because "@types#react-native" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3

In my case, for example, you can see that the versions of @types/react are 17.0.44 and 18.0.3.
Please note the version numbers you get may not be same as my case above.

Then you put following lines into package.json.

"resolutions": {
    "@types/react": "17.0.44"
},

Version 18 is not recommended because React Native is constructed on react v17.

I hope you successfully resolve this case. The root cause of this problem is incompatibility between @types/react v17 and @types/react v18. Not only with react native, many people encounter the similar issue with several packages which use @types/react.

BR,

@sakthiinfotec
Copy link

@orome,

Before using resolutions, I would recommend to confirm the version number of your @types/react by typing

yarn why @types/react

and you will get as follows:

=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info This module exists because it's specified in "devDependencies".
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
=> Found "@types/react-native#@types/react@18.0.3"
info This module exists because "@types#react-native" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3

In my case, for example, you can see that the versions of @types/react are 17.0.44 and 18.0.3. Please note the version numbers you get may not be same as my case above.

Then you put following lines into package.json.

"resolutions": {
    "@types/react": "17.0.44"
},

Version 18 is not recommended because React Native is constructed on react v17.

I hope you successfully resolve this case. The root cause of this problem is incompatibility between @types/react v17 and @types/react v18. Not only with react native, many people encounter the similar issue with several packages which use @types/react.

BR,

Thanks a lot. This fixed the issue. As I was using React version 17.0, and tried installing @types/react v18.0, is the reason my solution was not working.

Now installed both

"devDependencies": {
  "@types/react": "17.0.44",
}
   "resolutions": {
    "@types/react": "17.0.44"
  },

Thanks again!

@MartinGrue
Copy link

The only thing that worked for me was downgrading react and react-dom to v17.0.1

@orome,
Version 18 is not recommended because React Native is constructed on react v17.

@orome
Copy link

orome commented Apr 20, 2022

@challengy1 I guess I'm really confused about two things then:

  1. What is "resolutions" doing (esp. if it specifies the same version)?
  2. How do I discover this (that I need one version for Expo and a later version for TypeScript) when if this or a similar issue arises again?

And another issue, I guess:

  1. How on earth do I automate this?

@challengy1
Copy link
Author

Hi, Orome

I will try to answer your questions as follows:

(1) What is 'resolutions' doing?
'resolutions' specifies version to be used in the project. If you do 'yarn why @types/react' after you 'resolutions' is added to the package.json, you will see only one (specified version) is detected and another version is 'hosited' (means suppressed).

(2) How do I discover this?
I didn't know how to resolve this. So, I posted this question and got an answer from satya164, which was a big help. Also I found an issue as follows:
Bug: React 18 types broken since the type release a view hours ago #24304

I would recommend you to take a look at it.

Not only with React-native, when you see an error message 'XXX cannot be used as a JSX component.' You need to investigate if there are multiple @types/react.

(3) How on earth do I automage this?
I don't think it can be automated. Essentially, in this case, @types/react-native has to be fixed to specify version number of @types/react. Until react-native install process is fixed, putting 'resolutions' into package.json is the only way to resolve it.

BR, Challengy

@orome
Copy link

orome commented Apr 21, 2022

@challengy1 I still don't get what resolutions does then. Or more precisely what is devDependencies doing? Doesn't the latter already specify a version? Why does it need to be specified again for things to work properly?

@challengy1
Copy link
Author

Hi, Orome

I will give you some more detail information as follows. Firstly, I will focus on procedure through which you can (anyway) establish your development environment. Please take following procedure step by step.

(1) Cd to somewhere in which you plan to make your project.

(2) expo init <name_of_the_project> for example as follows:
>>> expo init sample
you will choose a template with Typescript.
blank (TypeScript) same as blank but with TypeScript configuration

(3) Cd to the project directory.

(4) Check @types/react dependency by typing as follows:
>>> yarn why @types/react

=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info This module exists because it's specified in "devDependencies".
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
=> Found "@types/react-native#@types/react@18.0.6"
info This module exists because "@types#react-native" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3
✨  Done in 0.58s.

You will fine TWO @types/react in the result. I will discuss this later.

(5) Add resolutions field into package.json as follows:

{
  "name": "sample",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-navigation/drawer": "^6.4.1",
    "@react-navigation/native": "^6.0.10",
    "expo": "~44.0.0",
    "expo-status-bar": "~1.2.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-gesture-handler": "~2.1.0",
    "react-native-reanimated": "~2.3.1",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.10.1",
    "react-native-web": "0.17.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "~17.0.21",
    "@types/react-native": "~0.64.12",
    "typescript": "~4.3.5"
  },
  "resolutions": {
    "@types/react": "17.0.44"
  },
  "private": true
}

I added only resolutions fields (with 3 lines as you see above.)

(6) You will install some navigation related packages as follows (for example drawer):

yarn add @react-navigation/native
expo install react-native-screens react-native-safe-area-context
yarn add @react-navigation/drawer
expo install react-native-gesture-handler react-native-reanimated

(7) When you use reanimated, you need to add a 'plugins' line into babel.config.js as follows:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin'],
  };
};

(8) With a test code as follows, you will confirm that the issue is resolved.

import { View, Text } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createDrawerNavigator } from "@react-navigation/drawer";

type StackParamList = {
  Home: undefined;
};

const Drawer = createDrawerNavigator<StackParamList>();

const HomeScreen = () => {
  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
      <Text>HOME PAGE</Text>
    </View>
  );
};

export default function App() {
  return (
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="Home">
        <Drawer.Screen
          name="Home"
          component={HomeScreen}
          options={{ title: "HOME " }}
        />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

(9) Kick start by typing as follows:
>>> yarn start


Now, I will explain a bit about version conflict.
Please take a look at the result of yarn why.

=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info This module exists because it's specified in "devDependencies".
...
=> Found "@types/react-native#@types/react@18.0.6"
info This module exists because "@types#react-native" depends on it.

@types/react@17.0.44 was installed because the project was built with typescript template.
@types/react@18.0.6 was (mistakenly) installed because of @types#react-native.

The important thing is that @types/react-native "by mistake" installs @types/react@18.0.6.
It should have install @types/react@17 because react-native is developed on top of React 17.

The root cause of this problem is that react-native does NOT specify the version number.
And the latest version of @types/react (version 18) was installed.
Unfortunately @types/react@18 is not compatible with @17.

resolutions in the package.json 'specifies' the version to be used in this project.

After adding resolution field, you will confirm as follows:

>>> yarn why @types/react
yarn why v1.22.17
warning ../../package.json: No license field
[1/4] 🤔  Why do we have the module "@types/react"...?
[2/4] 🚚  Initialising dependency graph...
[3/4] 🔍  Finding dependency...
[4/4] 🚡  Calculating file sizes...
=> Found "@types/react@17.0.44"
info Has been hoisted to "@types/react"
info Reasons this module exists
   - Specified in "devDependencies"
   - Hoisted from "@types#react-native#@types#react"
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
✨  Done in 0.32s.

Now this project uses @types/react@17.0.44 only.

I hope you can successfully establish your environment.

BR, Challengy

@challengy1
Copy link
Author

Just a piece of minor information.

On Package.json, the version number of @types/react is v17.0.21. But when your see yarn.lock file, you will find that v17.0.44 is actually installed. It is not necessary to stick on the version number mentioned in package.json.

BR, Challengy

@orome
Copy link

orome commented Apr 22, 2022

@challengy1 I still don't understand (sorry). I'd always thought that the whole purpose of packages.json was for the developer to have control over what versions of what packages get installed, and that in particular that is what dependencies or devDependencies are for: If I say that I want version X.Y.Z of somepackage in one of those then that's what I get.

But are you saying that in fact something else (what?) can install an arbitrary version of somepackage(where?) if it wants to, ignoring dependencies or devDependencies, and that in order to prevent that I need to specify my version constraints again in resolutions?

(FWIW, for me yarn why @types/react just lists one version: 17.0.44.

@challengy1
Copy link
Author

Hi, Orome

Any JavaScript / TypeScript project consists of many packages and each package has a lot of packages inside. This means we have a huge number of dependency-chains inside. Each package has its own package.json which specify name of the package with version number.
for example, please take a look at the file as follows:
node_modules/@types/react-native/package.json

In the file, you will witness @types/react as follows:

    "dependencies": {
        "@types/react": "*"
    },

The root cause of this problem is this "*" in the dependencies.
During installation, necessary packages specified in ALL package.json FILES is installed.
When version number is clearly specified, it will be installed. But, when "*" is specified, THE LATEST version of the package is installed. In this case it is version 18.0.6.

Again, one of the root causes is incompatibility between V17 and V18 @types/react and another cause is specifying "asterisk" for @types/react in the @types/react-native file.

I would recommend you to read all messages in the GitHub issue as follows:

facebook/react#24304

In this, you will read a message from KutnerUri as follows:

This breaks everything! 😭
I have a React 15 project that's happy with not upgrading to React 18 right away.
Because a LOT of packages depend on "@types/react": "*", version 18 is getting installed without anyone using it!

How long are you going to release this outdated @types/react which falls out of sync with the react package?
can you include built in types like other modern libraries?

Inserting resolutions in your project level package.json is one of the ways and you may find some other alternatives. Using resolutions is not idealistic but it is a pragmatic and effective way.

BR, Challengy1

@orome
Copy link

orome commented Apr 22, 2022

@challengy1 So the short answer is "yes"?

But are you saying that in fact something else (what?) can install an arbitrary version of somepackage(where?) if it wants to, ignoring dependencies or devDependencies, and that in order to prevent that I need to specify my version constraints again in resolutions?

If so it seems a bit of a mess: dependencies is just a wish, and can be ignored by other packages that get installed by other packages in the chain, and when those are discovered resolutions (really more like an "override") needs to be used to ensure that the version specified in dependencies is used?

If that's so why not just have an "I really mean it" flag that applies to dependencies to prevent such issues; or just duplicate every package listed in dependencies under resolutions?

@satya164
Copy link
Member

dependencies is just a wish, and can be ignored by other packages that get installed by other packages in the chain

No, your dependencies are installed and those versions are used when you import them into your code. If a package has another version of a dependency its package.json, it's installed for that package to use. Neither is ignored, both of the versions are installed.

If that's so why not just have an "I really mean it" flag that applies to dependencies to prevent such issues; or just duplicate every package listed in dependencies under resolutions

Different versions of a package may have different APIs/behavior. Forcing another version than what a package was tested with (and says it depends on) is likely to break things.

@challengy1
Copy link
Author

Thanks satya164,

Dear Orome,
Your project has many package.json files. You see only one of them on the top layer of the project. Yes, you can control something (not everything) through the top level package.json.
However, it is not realistic to modify all package.json files in your project. If you did so, those settings would be used until you update packages. As soon as you update (or updated automatically through CI cycle), the your modified package.json files will be overwritten. This means, changing package.json in @type/react-native cannot be a valid solution in this case.

BR, Challengy1

@orome
Copy link

orome commented Apr 23, 2022

@satya164 I can understand that other packages might need other versions of something my package has as a dependency, but if that's the case, why is my code exposed to the existence of that package? Maybe I'm coming from the wrong place altogether here. I would assume that there is some kind of LTS system of mutually compatible packages so that none of this would be an issue.

And why not always use yarn --flat?

@satya164
Copy link
Member

why is my code exposed to the existence of that package

Because some code relies on things in global scope - such as @types/react. For such packages, it might be problematic to mix 2 separate versions in the same app. But this case is the exception.

And for the cases where it's not possible to mix 2 separate versions, forcing a single version isn't guaranteed to fix the issue due to API differences - it can cause other issues. In this specific case it works fine.

I would assume that there is some kind of LTS system of mutually compatible packages so that none of this would be an issue.

LTS doesn't have anything to do with compatibility. It just means the maintainer provides long term support for the package.

There's a system called semver where there's API compatibility and forcing a version between matching semver won't break code. The package manager already takes care of deduplicating where the semver specified is compatible between 2 different versions and installs a single version instead. In this specific case react-native doesn't specify a proper version which ends up installing the latest version which is not semver compatible with the version you have in your package.json.

And why not always use yarn --flat

Like I said, it might break things due to API differences. It also doesn't make sense to do something which might fix an issue in rare cases but cause issues for majority of cases.

@orome
Copy link

orome commented Apr 23, 2022

@satya164 OK that clears things up a bit. I think what's confusing me is the lack of (what I was calling) LTS. What I meant was something like Stackage: something that ensures a collection of mutually compatible versions that work together. The React Native ecosystem seems to lack this, so maintaining dependencies is a bit of a free-for-all.

If that's the case, how does one ever know if versions will work together, or ensure that somehow an incompatible version isn't added (in global scope).

@AlienDev66
Copy link

Only update react to version 18.0.6 like:

"devDependencies": {
    "@types/react": "^18.0.6",
  },

@orome
Copy link

orome commented Apr 24, 2022

I've got:

  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/jest": "^27.4.1",
    "@types/react": "~17.0.44",
    "@types/react-native": "~0.64.24",
    "@typescript-eslint/eslint-plugin": "^5.20.0",
    "@typescript-eslint/parser": "^5.20.0",
    "eslint": "^8.14.0",
    "eslint-plugin-jest": "^26.1.5",
    "eslint-plugin-react": "^7.29.4",
    "eslint-plugin-react-hooks": "^4.4.0",
    "jest": "^27.5.1",
    "typescript": "~4.3.5"
  },
  "resolutions": {
    "@types/react": "17.0.44"
  },

and that works for me. Should I be using ^18.0.6 instead? A more recent version of typescript?

@ELBEQQAL94
Copy link

@captain-ray captain-ray Thank you!

@challengy1
Copy link
Author

@orome

I don't think it is a good idea to mix multiple versions of @types/react in your project.

BR,

@orome
Copy link

orome commented Apr 25, 2022

I don't think it is a good idea to mix multiple versions of @types/react in your project.

Wait what? I thought all this was about exactly that happening, beyond my control.

@kevinhaube
Copy link

For those having a similar issue with Stack.Navigator, it appears this is linked with expo's serving of the application. Something on their end must be reliant on @types/react v.17.0.21 because if any newer version is installed, it doesn't detect it and forces you to install that version before serving to an emulator.

@louiechristie
Copy link

louiechristie commented May 3, 2022

This worked for me (when using Expo SDK45 beta):

package.json :

{
  "name": "example",
...
  
  "devDependencies": {
    ...
    "@types/react": "~17.0.21",
   ...
  },
  "resolutions": { "@types/react": "~17.0.21"},
  "dependenciesComments": {
    "@types/react": "Resolution because of this issue: https://github.com/react-navigation/react-navigation/issues/10507"
  }
}

@lahirudx
Copy link

lahirudx commented May 16, 2022

Still having the issue with expo Typescript with Tabs template.
Found another @types/react version in yarn.lock by running,

yarn why @types/react

result:

=> Found "@types/react@17.0.45"
info Has been hoisted to "@types/react"
info Reasons this module exists
   - Specified in "devDependencies"
   - Hoisted from "@types#react-native#@types#react"
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
✨  Done in 0.22s.

Then,
added this line to package.json.

"resolutions": {
 "@types/react": "~17.0.45"
},

then,

yarn install

@SudhanshuBhagwat
Copy link

SudhanshuBhagwat commented Jun 2, 2022

Been struggling with the same issue
Found another @types/react version in yarn.lock by running,

yarn why @types/react

=> Found "@types/react@17.0.45"
info Has been hoisted to "@types/react"
info Reasons this module exists
   - "workspace-aggregator-3dbc7dec-544a-4289-916d-c759bc5b5c0e" depends on it
   - Hoisted from "_project_#vite-react-typescript-starter#@types#react"
   - Hoisted from "_project_#app#@types#react"
   - Hoisted from "_project_#vite-react-typescript-starter#@types#react-dom#@types#react"
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
=> Found "@types/react-native#@types/react@18.0.10"
info This module exists because "_project_#app#@types#react-native" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3
=> Found "@types/hoist-non-react-statics#@types/react@18.0.10"
info This module exists because "_project_#vite-react-typescript-starter#react-redux#@types#hoist-non-react-statics" depends on it.
info Disk size without dependencies: "196KB"
info Disk size with unique dependencies: "1.38MB"
info Disk size with transitive dependencies: "1.38MB"
info Number of shared dependencies: 3

My current react-native app has the following package.json contents

"devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "~17.0.45",
    "@types/react-native": "~0.66.13",
    "typescript": "~4.3.5"
  },
  "resolutions": {
    "@types/react": "17.0.45"
  },

Other react app's package.json

devDependencies": {
    "@types/react": "^17.0.45",
    "@types/react-dom": "^17.0.17",
    "@vitejs/plugin-react": "^1.3.0",
  }

And it is a monorepo, but I've checked that all of the package.json files have @types/react: 17.0.45

@masrurimz
Copy link

Still having the issue with expo Typescript with Tabs template. Found another @types/react version in yarn.lock by running,

yarn why @types/react

result:

=> Found "@types/react@17.0.45"
info Has been hoisted to "@types/react"
info Reasons this module exists
   - Specified in "devDependencies"
   - Hoisted from "@types#react-native#@types#react"
info Disk size without dependencies: "184KB"
info Disk size with unique dependencies: "1.37MB"
info Disk size with transitive dependencies: "1.37MB"
info Number of shared dependencies: 3
✨  Done in 0.22s.

Then, added this line to package.json.

"resolutions": {
 "@types/react": "~17.0.45"
},

then,

yarn install

It's not working, i am still got the problem on expo

@danielyaa5
Copy link

danielyaa5 commented Jun 7, 2022

Also still having this problem

  "dependencies": {
    "@buy-or-rent/common": "*",
    "@expo/vector-icons": "^13.0.0",
    "@react-navigation/bottom-tabs": "^6.0.5",
    "@react-navigation/native": "^6.0.2",
    "@react-navigation/native-stack": "^6.1.0",
    "expo": "~45.0.0",
    "expo-asset": "~8.5.0",
    "expo-constants": "~13.1.1",
    "expo-font": "~10.1.0",
    "expo-linking": "~3.1.0",
    "expo-splash-screen": "~0.15.1",
    "expo-status-bar": "~1.3.0",
    "expo-system-ui": "~1.2.0",
    "expo-web-browser": "~10.2.0",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-native": "0.68.2",
    "react-native-safe-area-context": "4.2.4",
    "react-native-screens": "~3.11.1",
    "react-native-web": "0.17.7"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@expo/webpack-config": "^0.16.24",
    "@types/jest": "^27",
    "@types/react": "~17.0.21",
    "@types/react-native": "~0.66.13",
    "@types/react-test-renderer": "17.0.2",
    "babel-plugin-transform-inline-environment-variables": "^0.4.4",
    "jest": "^27",
    "jest-expo": "^45.0.1",
    "react-native-monorepo-tools": "^1.1.4",
    "react-test-renderer": "17.0.2",
    "typescript": "~4.3.5"
  },
  "resolutions": {
    "@types/react": "17.0.45"
  },
yarn why @types/react
'
'
├─ @buy-or-rent/app@workspace:packages/app
│  └─ @types/react@npm:17.0.45 (via npm:~17.0.21)
│
├─ @types/react-native@npm:0.66.18
│  └─ @types/react@npm:18.0.12 (via npm:*)
│
└─ @types/react-test-renderer@npm:17.0.2
   └─ @types/react@npm:17.0.45 (via npm:^17)

It works if I remove the package @types/react package from devDependencies but then expo forces me to install it

@anarkrypto
Copy link

I had this problem at the expo and nothing resolved it, even with resolutions.
Because the expo always warned It looks like you're trying to use TypeScript but don't have the required dependencies installed. Would you like to install @types/react

Until I found the solution, using version 18.0.0 inside resolutions

"resolutions": {
    "@types/react": "18.0.0"
  }

juhyeongee added a commit to juhyeongee/cero-beta that referenced this issue Aug 10, 2022
react-navigation/react-navigation#10507
버전이슈니 뭐니 어쩌고 다 했는데,
그냥 import 다시 새로적어주면 되는 이슈였음 ;
  밤에 뭐했냐 진짜 ;;ㄱ허ㅏㅣ붖기ㅏ버
@mayuran-knod-dev
Copy link

delete node modules and yarn install again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests