我想請教一些建議。我是 React 的新手,所以我不知道這里的情況如何。所以我希望我的變數輸出這個: < Text>Zlin</ Text> < br> (在代碼中: lokace ='' mesta[i] '< /Text>' "\n" )。我該如何存檔?當我在 return() 中呼叫它時,它總是希望在 <Text> 中。
我將把它重新制作成在這個回圈中生成的輸出卡,所以我不能使用 <Text>。
import * as React from 'react';
import { StyleSheet, ScrollView, View, Text, Image } from 'react-native';
import { Card, CardTitle, CardContent, CardAction, CardButton, CardImage } from 'react-native-material-cards';
var mesta = ['Zlin','Praha','Ostrava','Brno']
var cardBeg = ['<Text>']
var cardEnd = ['</Text>']
var lokace = []
for (var i=0; i < mesta.length; i )
{
lokace ='<Text>' mesta[i] '</Text>' "\n"
}
export default function Primary({ navigation })
{
return(
<ScrollView style=
{{
flex: 1,
}}>
<View style={{ backgroundColor: "violet", borderRadius: 20, alignItems: 'center', margin: 5, padding: 10}}>
<Text style={{ fontSize: 26, fontWeight: 'bold' }}>
Swag
</Text>
{lokace} //this doesnt work
<Text>{lokace}</Text> //this does, but I dont want to output only text and how I said, I will later be outputting whole tables.
</View>
</ScrollView>
);
}
uj5u.com熱心網友回復:
如果你想在 React 中回圈資料,你通常會使用.map().
所以你只需要mesta在 JSX 中映射你的陣列。這是您需要做的簡化版本:
function Primary() {
return (
<ScrollView
<View>
<Text>Swag</Text>
{mesta.map(item => (
<Text>{item}</Text>
))}
</View>
</ScrollView>
)
}
另外,我建議不要var在宣告變數時使用。使用letorconst現在是最佳實踐。一般規則是let在您想要宣告一個可變值和const一個不可變值時使用(但這更多是個人選擇!)。
uj5u.com熱心網友回復:
我不知道我是否正確地得到了你的問題,但也許這就是你想要做的事情:
代替{lokace}
做
{mesta.map((m) => <Text> m </Text> )}
并洗掉 for 回圈
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422632.html
標籤:
