繁体   English   中英

React Native 给出错误,即变量未定义时

[英]React Native giving error that variable is not defined when it is

我已经使用 require() 链接了一个外部 JS 文件,它甚至已经识别了它。 当我从那个外部文件调用 function 时,它会指示 function 已被识别,但它仍然会给出错误,它找不到变量(在我的例子中是一个名为 text() 的 function)。 我的 App.js:

require('./comp/functions.js')
import React from 'react'
import {View, Text, StyleSheet, Button} from 'react-native'


export default function App() {
      return(<>
      <View style={styles.loginbox}>
        <Text style={{textAlign: "center", fontWeight: "bold", fontSize: 30}}>LOGIN</Text>
        <Button title="Login Now!" onPress={test}/>

      </View>
      </>)
}

const styles = StyleSheet.create({
   loginbox: {
     position: "relative",
     top: 100
   }
})

功能.js:

function test() {
    alert(123)
  }

我希望它在立即登录时运行 test() function! 按钮已被按下

您需要先从functions.js中导出函数。 然后,您可以import其导入您的应用程序。 以下应该工作。

函数.js

export default function test() {
  alert(123);
}

应用程序.js

import test from "./functions";
import React from "react";
import { View, Text, StyleSheet, Button } from "react-native";

export default function App() {
  return (
    <>
      <View style={styles.loginbox}>
        <Text style={{ textAlign: "center", fontWeight: "bold", fontSize: 30 }}>
          LOGIN
        </Text>
        <Button title="Login Now!" onPress={test} />
      </View>
    </>
  );
}

const styles = StyleSheet.create({
  loginbox: {
    position: "relative",
    top: 100
  }
});

暂无
暂无

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

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