[英]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.