【发布时间】:2018-08-17 14:36:43
【问题描述】:
我想在 react native 中添加单选按钮,这很简单,我已经做到了。但问题是我想在从服务器获取内容时循环通过单选按钮。所以列表取决于正在更改的获取项目的数量。我获取的数据如下
json 数据
{
"data": [{
"content": "question",
"selection": [
"(a).option1",
"(b).option2 ",
"(c).option3 ",
"(d).option4 "
],
"correct": "4",
}]
}
好吧,我在CardSection 组件内显示content。我想用相应的单选按钮循环selection 数组。我正在使用映射方法来呈现data。那么对于selection,我应该使用什么? for 循环可以吗?但我不知道该怎么做,请帮忙。以下是反应部分。
更新
const formatArray = (arr) => {
let newArr = []
arr.map((item, index) => newArr.push({'label': index, value: item}))
return newArr
}
class QASection extends Component{
render() {
_setActive = (active) => this.setState({this.setState({selected:value})})
return(
{this.state.details.map(a =>
<Card>
<CardSection>
<Text>{a.content}</Text>
</CardSection>
<CardSection>
<RadioForm
radio_props={radio_props}
initial={0}
itemShowKey="label"
itemRealKey="value"
dataSource={formatArray(data)}
onPress={onPress}
/>
<CardSection data={data} key={index} onPress={this._setActive} />
</Card>
)}
);
}
}
通过渲染selection[0],它会给我selection数组中的第一个值。但是如何使用数组中每个项目的单选按钮循环遍历呢?
【问题讨论】:
-
这不是我要找的那个。这里怎么循环json数据?
标签: javascript for-loop react-native radio-button jsx