如何在 React Native 中嵌套循环?
How to nested loop in react native?
我有 json 并且我想在 React Native 中嵌套循环。我如何在 Accordion 中嵌套 while SECTIONS?
const SECTIONS = [
{
title: 'Monday',
content: {
'1':'washing dish'
'2':'work'
},
},
{
title: 'Tuesday',
content: 'Lorem ipsum...',
},
{
title: 'Saturday',
content: 'Lorem ipsum...',
},
];
_renderContent = section => {
return (
<View style={styles.contentCon}>
<Text>{section.content}</Text>
</View>
);
};
<Accordion
activeSections={this.state.activeSections}
sections={SECTIONS}
renderHeader={this._renderHeader}
renderContent={this._renderContent}
onChange={this._updateSections}
/>
如何循环渲染内容以得到洗碗和工作?
您可以使用 Object.keys
或 Object.values
:
遍历一个对象
renderContent = (section) => {
const { content } = section;
if (typeof content === 'string') {
return (
<View style={styles.contentCon}>
<Text>{section.content}</Text>
</View>
);
}
return (
<View style={styles.contentCon}>
{Object.keys(content).map(key => (
<Text key={key}>
{content[key]}
</Text>
))}
</View>
);
}
我有 json 并且我想在 React Native 中嵌套循环。我如何在 Accordion 中嵌套 while SECTIONS?
const SECTIONS = [
{
title: 'Monday',
content: {
'1':'washing dish'
'2':'work'
},
},
{
title: 'Tuesday',
content: 'Lorem ipsum...',
},
{
title: 'Saturday',
content: 'Lorem ipsum...',
},
];
_renderContent = section => {
return (
<View style={styles.contentCon}>
<Text>{section.content}</Text>
</View>
);
};
<Accordion
activeSections={this.state.activeSections}
sections={SECTIONS}
renderHeader={this._renderHeader}
renderContent={this._renderContent}
onChange={this._updateSections}
/>
如何循环渲染内容以得到洗碗和工作?
您可以使用 Object.keys
或 Object.values
:
renderContent = (section) => {
const { content } = section;
if (typeof content === 'string') {
return (
<View style={styles.contentCon}>
<Text>{section.content}</Text>
</View>
);
}
return (
<View style={styles.contentCon}>
{Object.keys(content).map(key => (
<Text key={key}>
{content[key]}
</Text>
))}
</View>
);
}