私はreactNativeの新しい蜂です。繰り返し質問する場合はご容赦ください。このトピックに関して以前に尋ねられた質問は私には役に立たないので、私は相対的な質問をしています。JSON応答に基づいて動的チェックボックスを作成したいのですが、データを配列に収集しました。以下はコードです。
sellablePartsCategory = [];
SellablePartsCategoryにはデータが含まれています
sellablePartsCategory = ["Brake", "Suspension", "Engine", "Tyre"]
renderParts(){
for(let i=0; i<sellablePartsCategory.length; i++){
return(
<CheckBoxComponent
title={sellablePartsCategory[i]}
checked= {true} /> );
}
}
render(){
<View>
<Text> {sellablePartsCategory} </Text>
{this.renderParts()}
</View> }
returnステートメントがforループを壊し、1回だけ実行することを知っています。配列のインデックス値がゼロになり、ループが中断します。問題を解決する方法がわかりません。CheckBoxComponent.jsは
import React, { Component } from 'react';
import {TextInput, StyleSheet, View, Text} from 'react-native';
import { CheckBox, ListItem, List } from 'react-native-elements';
const CheckBoxComponent = ({title, checked}) => {
return (
<CheckBox
title={title}
checkedColor='#0D4A8E'
checked={checked}
/>
);};
export { CheckBoxComponent };
あなたにはrenderParts()
この方法、あなたは、単一のリターンを使用して、リスト内のすべてのチェックボックスを返すことができます。以下のコードを試してください。
renderParts(){
let checkBoxComponentList = [];
for(let i=0; i<sellablePartsCategory.length; i++){
checkBoxComponentList.push(<CheckBoxComponent
title={sellablePartsCategory[i]}
checked= {true} />);
}
return checkBoxComponentList;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加