为什么在React Native中将字符串对象传递给函数时会得到 ‘undefined’?

huangapple go评论71阅读模式
英文:

Why do I get 'undefined' when passing string objects to a function in React Native?

问题

我对JS和RN都相当新,所以这可能是一个超级愚蠢的问题。我有一些字符串对象,表示用户的登录详细信息,这些信息从一个组件传递到另一个文件中的另一个函数。

loginButton.js

// 在提交按钮被按下时调用的函数
async function submitInfo( name, id, grade, email, password, { navigation }) {
    await accountChecks(name, email, password).then(() => {       
        // 做一些事情
    }).catch((error) => {
        Alert.alert(error);   
    });
}

// 提交按钮组件
function InfoSubmit( { name, id, grade, email, password, navigation} ) {
    return (
        <Pressable style={styles.submitButton} onPress={() => submitInfo(name, id, grade, email, password, {navigation})}>
            <Text style={styles.submitButtonText}>
                提交
            </Text>
        </Pressable>
    );
}

函数submitInfo调用了accountChecks,其代码如下:

function accountChecks(name, email, password) {
    // 记录数据和数据类型
    console.log(name, email, password);
    console.log(typeof(name), typeof(email), typeof(password));

    // 执行账户检查操作
}

export default accountChecks();

问题是,accountChecks.js 中的这些console.log()语句输出如下:

LOG undefined undefined undefined

LOG undefined undefined undefined

如果这个问题很愚蠢的话,再次道歉,因为我还是相对新手。

老实说,我真的不知道该尝试什么,我已经研究了stackoverflow以及其他帮助论坛,似乎没有人有与我相同的问题,这让我相信这可能是一个愚蠢的问题,或者是一个只有我遇到的问题。

英文:

I'm pretty new to JS and RN in general so this might be a super dumb question. I have some string objects representing a user's login details that are passed from a component on click to another function in a separate file.

loginButton.js

// Function called on submit button pressed
async function submitInfo( name, id, grade, email, password, { navigation }) {
    await accountChecks(name, email, password).then(() =&gt; {       
        // Do stuff
    }).catch((error) =&gt; {
        Alert.alert(error);   
    });
}

// Submit button component
function InfoSubmit( { name, id, grade, email, password, navigation} ) {
    return (
        &lt;Pressable style={styles.submitButton} onPress={() =&gt; submitInfo(name, id, grade, email, password, {navigation})}&gt;
            &lt;Text style={styles.submitButtonText}&gt;
                submit
            &lt;/Text&gt;
        &lt;/Pressable&gt;
    );
}

The function submitInfo calls accountChecks which looks like this:

function accountChecks(name, email, password) {
    // Log data and datatypes
    console.log(name, email, password);
    console.log(typeof(name), typeof(email), typeof(password));

    // Do account check stuff
}

export default accountChecks();

The issue is that these console.log() statements in accountChecks.js output this:

LOG undefined undefined undefined

LOG undefined undefined undefined

Again, sorry if this is a stupid question I'm still pretty new.

I honestly don't really know what to try, I've researched stackoverflow as well as other help forums and nobody seems to have the same problem as mee which leads me to believe that It's either a stupid problem or one unique to me.

答案1

得分: 0

感谢 Nick Parsons 提供的简单而快速的答案:

在函数调用之后添加 (),这会调用该函数,然后导出该函数的返回值。而不是这样做:export default accountChecks();,应该这样做:export default accountChecks; 以导出该函数。

英文:

Thanks to Nick Parsons for the simple and quick answer:

> Adding () after a function calls it, so export default accountChecks(); is calling your function and then exporting the return value of that function. Instead, do export default accountChecks; to export the function.

huangapple
  • 本文由 发表于 2023年5月28日 13:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定