質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

Q&A

解決済

1回答

3866閲覧

「'Stack.Navigator' を JSX コンポーネントとして使用することはできません。」のエラーを解決したい

ap2c9w

総合スコア40

React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

1グッド

0クリップ

投稿2022/05/18 10:55

React Nativigationを利用するためにVisual Studio Codeで以下のように書いたところ、<Stack.Navigator>でエラーが発生します。

typescript

1import { createStackNavigator } from '@react-navigation/stack'; 2import HomeScreen from '../screens/HomeScreen'; 3import ShopScreen from '../screens/ShopScreen'; 4import { StackParamList } from '../types/Stack'; 5const Stack = createStackNavigator<StackParamList>(); 6const HomeStackNavigator = () => ( 7 <Stack.Navigator> // ← この部分で赤い波下線がでる 8 <Stack.Screen 9 name="Home" 10 component={HomeScreen} 11 options={{ headerShown: false }} 12 /> 13 <Stack.Screen name="Shop" component={ShopScreen} /> 14 </Stack.Navigator> 15); 16export default HomeStackNavigator;

[エラーの内容]

'Stack.Navigator' を JSX コンポーネントとして使用することはできません。 その要素の型 'ReactElement<any, any> | Component<Omit<DefaultRouterOptions<string> & { children: ReactNode; screenOptions?: StackNavigationOptions | ((props: { ...; }) => StackNavigationOptions) | undefined; defaultScreenOptions?: StackNavigationOptions | ... 1 more ... | undefined; } & StackRouterOptions & StackNavigationConfig,...' は有効な JSX 要素ではありません。 型 'Component<Omit<DefaultRouterOptions<string> & { children: ReactNode; screenOptions?: StackNavigationOptions | ((props: { route: RouteProp<...>; navigation: any; }) => StackNavigationOptions) | undefined; defaultScreenOptions?: StackNavigationOptions | ... 1 more ... | undefined; } & StackRouterOptions & StackNavigat...' を型 'Element | ElementClass | null' に割り当てることはできません。 型 'Component<Omit<DefaultRouterOptions<string> & { children: ReactNode; screenOptions?: StackNavigationOptions | ((props: { route: RouteProp<...>; navigation: any; }) => StackNavigationOptions) | undefined; defaultScreenOptions?: StackNavigationOptions | ... 1 more ... | undefined; } & StackRouterOptions & StackNavigat...' を型 'ElementClass' に割り当てることはできません。 'render()' によって返された型は、これらの型同士で互換性がありません。 型 'React.ReactNode' を型 'import("/Users/hiro/shop-review-app/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode' に割り当てることはできません。 型 '{}' を型 'ReactNode' に割り当てることはできません。ts(2786)

[package.json]

json

1{ 2 "name": "shop-review-app", 3 "version": "1.0.0", 4 "main": "node_modules/expo/AppEntry.js", 5 "scripts": { 6 "start": "expo start", 7 "android": "expo start --android", 8 "ios": "expo start --ios", 9 "web": "expo start --web", 10 "eject": "expo eject", 11 "lint": "eslint 'App.tsx'", 12 "lint:fix": "eslint --fix 'src/**/*.{js,jsx,ts,tsx}'", 13 "preinstall": "typesync || :" 14 }, 15 "dependencies": { 16 "@expo/vector-icons": "^13.0.0", 17 "@react-native-community/masked-view": "^0.1.11", 18 "@react-navigation/bottom-tabs": "^5.x", 19 "@react-navigation/native": "5.9.6", 20 "@react-navigation/stack": "^5.x", 21 "@typescript-eslint/parser": "^5.24.0", 22 "eslint-plugin-import": "^2.26.0", 23 "eslint-plugin-jsx-a11y": "^6.5.1", 24 "eslint-plugin-react-hooks": "^4.5.0", 25 "expo": "~45.0.0", 26 "expo-constants": "^13.1.1", 27 "expo-status-bar": "~1.3.0", 28 "firebase": "7.24.0", 29 "react": "17.0.2", 30 "react-dom": "17.0.2", 31 "react-native": "0.68.2", 32 "react-native-gesture-handler": "~2.2.1", 33 "react-native-reanimated": "~2.8.0", 34 "react-native-safe-area-context": "4.2.4", 35 "react-native-screens": "~3.11.1", 36 "react-native-web": "0.17.7" 37 }, 38 "devDependencies": { 39 "@babel/core": "^7.12.9", 40 "@types/babel__core": "^7.1.19", 41 "@types/eslint": "^8.4.2", 42 "@types/prettier": "^2.6.1", 43 "@types/react": "~17.0.21", 44 "@types/react-dom": "17.0.2", 45 "@types/react-native": "~0.66.13", 46 "@typescript-eslint/eslint-plugin": "^5.24.0", 47 "eslint": "^7.32.0", 48 "eslint-config-airbnb": "^19.0.4", 49 "eslint-config-prettier": "^8.5.0", 50 "eslint-plugin-react": "^7.28.0", 51 "prettier": "^2.6.2", 52 "typescript": "~4.3.5" 53 }, 54 "private": true 55}

本エラーを解消するにはどのようにしたらよいでしょうか。

ams2020👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ap2c9w

2022/05/19 01:23

ありがとうございます! 示して頂いたgithubと同じ解決方法で解決いたしました。 @typesが重複してインストールされている可能性があるところまではたどり着いたのですが、packege.jsonだけ見ると重複してインストールされていることに気が付きませんでした。 yarn whyを叩く必要があると認識いたしました。 もし、よろしければ回答欄に同じことを書いていだけると嬉しいです。
ap2c9w

2022/05/19 01:23

hiro@hiroMacBookAir shop-review-app % yarn why @types/react yarn why v1.22.18 [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.45" 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-vector-icons#@types/react@18.0.9" info Reasons this module exists - "@types#react-native-vector-icons" depends on it - Hoisted from "@types#react-native-vector-icons#@types#react-native#@types#react" 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/react-dom#@types/react@18.0.9" info This module exists because "@types#react-dom" 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/react-native#@types/react@18.0.9" 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 1.17s.
ap2c9w

2022/05/19 01:25

上記のとおり、 @types/react@17.0.45 @types/react@18.0.9 で型定義ファイルが依存関係(でしょうか…)で重複してインストールされておりました。
ap2c9w

2022/05/19 01:25

hiro@hiroMacBookAir shop-review-app % yarn remove @types/react yarn remove v1.22.18 [1/2] 🗑 Removing module @types/react... [2/2] 🔨 Regenerating lockfile and installing missing dependencies... warning "react-native > react-native-codegen > jscodeshift@0.13.1" has unmet peer dependency "@babel/preset-env@^7.1.6". success Uninstalled packages. ✨ Done in 11.11s.
ap2c9w

2022/05/19 01:26

yarn remove @types/reactしてエラーが解消しております。 その後、yarn add @types/reactしたところ、@types/react@18.0.9がインストールされました。
guest

回答1

0

自己解決

https://github.com/react-navigation/react-navigation/issues/10507

hiro@hiroMacBookAir shop-review-app % yarn why @types/react yarn why v1.22.18 [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.45" 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-vector-icons#@types/react@18.0.9" info Reasons this module exists - "@types#react-native-vector-icons" depends on it - Hoisted from "@types#react-native-vector-icons#@types#react-native#@types#react" 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/react-dom#@types/react@18.0.9" info This module exists because "@types#react-dom" 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/react-native#@types/react@18.0.9" 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 1.17s.

上記のとおり、
@types/react@17.0.45
@types/react@18.0.9
で型定義ファイルが依存関係(でしょうか…)で重複してインストールされておりました。

hiro@hiroMacBookAir shop-review-app % yarn remove @types/react yarn remove v1.22.18 [1/2] ?? Removing module @types/react... [2/2] ?? Regenerating lockfile and installing missing dependencies... warning "react-native > react-native-codegen > jscodeshift@0.13.1" has unmet peer dependency "@babel/preset-env@^7.1.6". success Uninstalled packages. ? Done in 11.11s.

yarn remove @types/reactしてエラーが解消しております。
その後、yarn add @types/reactしたところ、@types/react@18.0.9がインストールされました。

投稿2022/05/23 23:06

ap2c9w

総合スコア40

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問