简体   繁体   English

无法在本机反应中导入.js文件

[英]can't import .js files in react native

screenshot截屏

I am trying to import these three components from the component folder but it doesnt let me.我正在尝试从组件文件夹中导入这三个组件,但它不允许我这样做。 The import statements are greyed out and doesn't read my.js files.导入语句显示为灰色并且不读取 my.js 文件。 I dont think its my file path at all because i was able to import an image the same way from asset folder.我根本不认为它是我的文件路径,因为我能够以相同的方式从资产文件夹导入图像。 Any help would be appreciated!任何帮助,将不胜感激!

app.js应用程序.js

import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import { Button, ImageBackground, SafeAreaView, StyleSheet, Text, TouchableWithoutFeedback, View } from "react-native";

import header from "./components/header";
import item from "./components/items";
import add from "./components/add";

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <ImageBackground source={require("./assets/cart.jpg")} style={styles.image}></ImageBackground>
      <StatusBar style="auto" />

      <Text style={styles.title}> Smart Shopper </Text>
      <Button title="Create A Shopping List" style={styles.button} />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
  },
  title: {
    fontSize: 50,
    fontFamily: "Roboto",
    fontWeight: "100",
    color: "black",
    bottom: 250,
  },
  image: {
    width: "100%",
    height: "100%",
    position: "absolute",
  },
});

header.js header.js

import React from "react";
import { View, Text, StyleSheet } from "react-native";

export default function header() {
  return (
    <View style={styles.header}>
      <Text style={styles.title}>My Todos</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    height: 80,
    paddingTop: 38,
    backgroundColor: "coral",
  },
  title: {
    textAlign: "center",
    color: "#fff",
    fontSize: 20,
    fontWeight: "bold",
  },
});

This is not actually an error.这实际上不是一个错误。 This is displaying in greyed out because VS formatter is not recognizing it's a react component because you used all small letters in your component name.这显示为灰色,因为 VS 格式化程序无法识别它是一个反应组件,因为您在组件名称中使用了所有小写字母。 Your react component should be in CamelCase.你的反应组件应该在 CamelCase 中。 Everyone is recommended to use Camel Case name in react component.建议大家在 react 组件中使用 Camel Case 名称。

So first change all of your component names and files names in camel case like this:因此,首先以驼峰形式更改所有组件名称和文件名,如下所示:

Change your file name Header.js更改文件名Header.js

import React from "react";
import { View, Text, StyleSheet } from "react-native";

export default function Header() {
  return (
    <View style={styles.header}>
      <Text style={styles.title}>My Todos</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    height: 80,
    paddingTop: 38,
    backgroundColor: "coral",
  },
  title: {
    textAlign: "center",
    color: "#fff",
    fontSize: 20,
    fontWeight: "bold",
  },
});

Do this for all components.对所有组件执行此操作。

Now, import it in your app.js like this:现在,像这样在你的 app.js 中导入它:

import Header from "./components/Header";
import Item from "./components/Items";
import Add from "./components/Add";

If we are supposed to create components then we must have to create components with the name that starts with the upper case.如果我们要创建组件,那么我们必须创建名称以大写开头的组件。

and also keep in mind that we also have to import the component with the first letter in upperCase.还要记住,我们还必须以大写的第一个字母导入组件。

Have a try at small changes in your code:尝试对您的代码进行小的更改:

in header.js在 header.js

export default function Header() {

in App.js在 App.js 中

import Header from "./components/header";

Keep in mind you must have to import the file with the filename you created.请记住,您必须使用您创建的文件名导入文件。 so, here import the Header with the header file name.所以,这里用 header 文件名导入 Header。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM