0

i have json and i want to nested loop in react native. how can i nested while SECTIONS in Accordion?

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}
                    />

how to loop render content to get washing dish and work?

1 Answer 1

1

You could iterate through an object using Object.keys or 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>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.