简体   繁体   English

为什么这个 JSX 代码在 return 语句中报告为不可访问?

[英]Why is this JSX code reported as unreachable in the return statement?

In my react native function component I have the following function:在我的反应原生 function 组件中,我有以下 function:

const renderStatus = () => {
    if (status == Status.PENDING) {
        return
        (
            <Text>{Texts.statusPending}</Text>
        )
    }
    if (status == Status.DONE) {
        return
        (
            <Text>{Texts.statusDone}</Text>
        )
    }
    if (status == Status.UPLOADING) {
        return
        (
            <>
                <Text>{Texts.statusUploading}</Text>
                <Loader style={{ margin: 8 }} />
            </>
        )
    }
    if (status == Status.FAILED) {
        return
        (
            <>
                <Text>{Texts.statusFailed}</Text>
                <Text style={{ color: 'Crimson' }}>{model.error}</Text>
            </>
        )
    }
}

For each of the return VSCode reports that JSX inside brackets is not reachable, and the function actually behaves like that, returns undefined .对于每个return的 VSCode 报告,括号内的 JSX 不可访问,并且 function 实际上的行为是这样的,返回undefined

VSCode 问题

What am I doing wrong?我究竟做错了什么?

You should put brackets after return like:您应该在 return 之后加上括号,例如:

 return( <Text>{Texts.statusPending}</Text> )

because it stops on return and don't go to the next line.因为它在返回时停止并且不要 go 到下一行。 For future use code formatters so it will automatically format your code every time you save the file.供将来使用代码格式化程序,因此每次保存文件时它都会自动格式化您的代码。 Most popular is prettier .最受欢迎的是更漂亮 So you will not have problems like that because it will format your code in good way right after you click CTRL+S所以你不会有这样的问题,因为它会在你点击 CTRL+S 后很好地格式化你的代码

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

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