【问题标题】:defining expo entryPoint in app.json does not work在 app.json 中定义 expo entryPoint 不起作用
【发布时间】:2021-06-11 11:41:33
【问题描述】:

我正在构建一个 React-Native android 应用程序。我想将 App.js 文件移动到 src 文件夹以获得更清晰的文件结构,因此我将此行添加到 expo 对象内的 app.json 文件中:

{
  "expo": {
    "entryPoint": "./src/App.js",

当我运行应用程序时,我会得到相同的旧默认 expo 应用程序。我在文件中有什么并不重要。当我更改任何内容时,它会引发此错误:

Unable to resolve "../../App" from "node_modules/expo/AppEntry.js"

我还尝试按照建议将这一行添加到文件中 here:

export default Expo.registerRootComponent(App);

我正在使用 Linux 和 android studio 模拟器。

【问题讨论】:

    标签: android linux react-native


    【解决方案1】:

    每当我尝试这样做时,对我有用的就是导入 registerRootComponent 并以下列方式使用它:

    这里是一个很简单的App.js

    import React from 'react';
    import { Text, View, StyleSheet } from 'react-native';
    import { registerRootComponent } from 'expo'; // import it explicitly
    
    class App extends React.Component {
      render () {
        return (
          <View style={styles.container}>
            <Text>Hello</Text>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'white'
      }
    });
    
    export default registerRootComponent(App); // this is how I register the App component
    

    这是我的app.json

    {
      "expo": {
        "entryPoint": "./src/App.js", <---- notice my entry point
        "name": "ExpoTester",
        "slug": "ExpoTester",
        "privacy": "public",
        "sdkVersion": "32.0.0",
        "platforms": [
          "ios",
          "android"
        ],
        "version": "1.0.0",
        "orientation": "portrait",
        "icon": "./assets/icon.png",
        "splash": {
          "image": "./assets/splash.png",
          "resizeMode": "contain",
          "backgroundColor": "#ffffff"
        },
        "updates": {
          "fallbackToCacheTimeout": 0
        },
        "assetBundlePatterns": [
          "**/*"
        ],
        "ios": {
          "supportsTablet": true
        }
      }
    }
    

    这是我的package.json

    {
      "name": "NewApp",
      "version": "0.0.0",
      "description": "No description",
      "author": null,
      "private": true,
      "main": "node_modules/expo/AppEntry.js",
      "dependencies": {
        "expo": "^32.0.0",
        "react": "16.5.0",
        "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
      }
    }
    

    这是我的文件结构:

    ├── app.json
    ├── assets
    │   ├── icon.png
    │   └── splash.png
    ├── package-lock.json
    ├── package.json
    └── src
        └── App.js <- notice the App.js is no longer in the root directory it is now in src
    

    此外,当对应用程序的结构进行任何重大更改时,我喜欢关闭捆绑程序,然后使用 expo start -c 重新启动它,以确保清理缓存。

    【讨论】:

    • 很烦人,将 import 语句从隐式更改为显式有效。非常感谢你,安德鲁。
    • 我收到了这个错误:[Unhandled promise rejection: Error: Unable to activate keep awake],知道吗?
    【解决方案2】:

    好吧,对于那些遇到此错误的人:

    [Unhandled promise rejection: Error: Unable to activate keep awake].

    基于上面Andrew提供的thisanswer,我找到了一种新的方法来实现重组。

    这是我的结构:

    .expo
      |- many_files
    .expo-shared
      |- many_files
    assests
      |- many_files
    node_modules
    src
      |- App.tsx
    app.json
    package.json
    ... (many more)
    

    这是我的package.json

    {
      "main": "./src/App.tsx",
      "scripts": {
        "start": "expo start",
        "android": "expo start --android",
        "ios": "expo start --ios",
        "web": "expo start --web",
        "eject": "expo eject"
      },
      "dependencies": {
        "expo": "~41.0.1",
        "expo-status-bar": "~1.0.4",
        "react": "16.13.1",
        "react-dom": "16.13.1",
        "react-native": "https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz",
        "react-native-web": "~0.13.12"
      },
      "devDependencies": {
        "@babel/core": "^7.9.0",
        "@types/react": "~16.9.35",
        "@types/react-native": "~0.63.2",
        "typescript": "~4.0.0"
      },
      "private": true
    }
    

    我的app.json

    {
      "expo": {
        "entryPoint": "./src/App.tsx",
        "name": "front",
        "slug": "front",
        "version": "1.0.0",
        "orientation": "portrait",
        "icon": "./assets/icon.png",
        "splash": {
          "image": "./assets/splash.png",
          "resizeMode": "contain",
          "backgroundColor": "#ffffff"
        },
        "updates": {
          "fallbackToCacheTimeout": 0
        },
        "assetBundlePatterns": [
          "**/*"
        ],
        "ios": {
          "supportsTablet": true
        },
        "android": {
          "adaptiveIcon": {
            "foregroundImage": "./assets/adaptive-icon.png",
            "backgroundColor": "#FFFFFF"
          }
        },
        "web": {
          "favicon": "./assets/favicon.png"
        }
      }
    }
    

    最后,我的App.tsx

    import React from 'react'
    import { StyleSheet, Text, View } from 'react-native'
    import { StatusBar } from 'expo-status-bar'
    import { registerRootComponent } from 'expo'
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center'
      }
    })
    
    const App = () => {
      return (
        <View style={styles.container}>
          <Text>Open up App.tsx to start working on your app!</Text>
          <StatusBar style='auto' />
        </View>
      )
    }
    
    export default registerRootComponent(App)
    

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2021-10-02
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多