Else if语句在React Native中返回



我是react-native的新手,我有一个问题,如何使其他如果在返回中,目前我有错误说,我想如果ScanResult if语句为真。

false is not a function (near`...Statement is true"))(ScanResult && _reac...`)

这只是一个例子但我有一个按钮来改变useState的布尔状态。谢谢你的进步,急需帮助

const [scan, setScan] = useState(false);
const [ScanResult, setScanResult] = useState(false);
const [result, setResult] = useState(null);

{(scan && (
<Text>Statement is true</Text>       //if true goes here
))                                  
(ScanResult && (
<Text>Scan Result is true</Text>   //else if true goes here
))                                   

|| (
<Text>Statement is false</Text>    //else goes here
)}                 

我想这样做:

const [scan, setScan] = useState(false);
const [ScanResult, setScanResult] = useState(false);
const [result, setResult] = useState(null);
return (scan ? <Text>Statement is true</Text> : ScanResult ? <Text>Scan Result is true</Text> : <Text>Statement is false</Text>)

如果您希望同时显示scanScanResult为真并且最后一条语句为真,则需要父元素。尝试将所有内容都包装在一个片段中,并使用以下代码:

return (
<>
{scan && <Text>Statement is true</Text>}
{ScanResult && <Text>Scan Result is true</Text>}                                   
{!scan && !ScanResult && <Text>Statement is false</Text>}
</>
)          

如果scan等于true,则呈现Statement is true。如果ScanResult为true,则渲染Scan Result is true。如果两个标志都为假,则呈现Statement is false

最新更新