[英]Looping And Rendering In JSX
I Need To Loop Through An Array-Like Object And Render Components In React Native According To It's Length And The And In It Here Is The Code:我需要循环遍历一个类似数组的对象并根据它的长度在 React Native 中渲染组件,其中的代码如下:
const Screen = function({navigation, route}) {
var data = [];
var cases = [];
axios.get(`${uri}cases`)
.then(json => {
console.log(json)
data = json.data
}).catch(err => {
console.log(err)
});
function showCases(cases){
cases.forEach(one => {
cases.push(<Text>one._id</Text>)
})
}
showCases(data)
var comp = [<Text>Hello</Text>, <Text>Hello</Text>]
return(
<View>
<View>
<Button title='Click' onPress={() => {
cases.push(<Text>Hello</Text>)
console.log(cases)
console.log(comp)
console.log(comp == cases)
}} />
<Button
title="Modern Clinic"
disabled={true}
/>
<Button title="Profit" onPress={() => {
navigation.push('Profit')
}} />
<Button title="Expenses" onPress={() => {
navigation.push('Expenses')
}}/>
<Button title="New" onPress={() => {
navigation.push('New...')
}}/>
</View>
<SafeAreaView>
<ScrollView>
<Text>{cases}</Text>
</ScrollView>
</SafeAreaView>
</View>
)
}
I just want to Loop Through The data
variable And Render Components I just want the logic so rendering a <Text />
component is okay just The Logic我只想循环遍历data
变量和渲染组件我只想要逻辑所以渲染<Text />
组件是好的只是逻辑
You need to use FlatList
which is the most used Component to display List
in React, It's like a Foreach
.你需要使用FlatList
,它是 React 中最常用的显示List
组件,它就像一个Foreach
。
Pass your data in the argument data.在参数数据中传递您的数据。 And in your renderItem
place your component.并在您的renderItem
放置您的组件。 In your component, access the value of foreach
with {item.Something}
, in this example data are : this.state.listParam = {.{"des":"Name","value":"John","mandatory":true, ...} }
在您的组件中,使用{item.Something}
访问foreach
的值,在此示例中,数据为: this.state.listParam = {.{"des":"Name","value":"John","mandatory":true, ...} }
<FlatList
style={{ marginBottom: 40 }}
data={this.state.listParam}
keyExtractor={(item,i) => i.toString()}
renderItem={({item,index}) => {
return(
<HiddenMandatoryInput
style={ style.textinput }
placeholder={item.des}
value={item.value}
mandatory={item.mandatory}
visible={item.visible}
onChangeText={(text) => this._changeText(text, item.name)}
type={item.type}
/>
)
}}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.