簡體   English   中英

如何在響應本機中嵌套循環?

[英]How to nested loop in react native?

我有json,我想在響應本機中嵌套循環。 手風琴中的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.keysObject.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>
  );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM