Skip to content
Advertisement

Trying to build React Component that can iterate over a nested data structure and produce styled HTML elements based on type

I have a nested data structure I’m getting back from an API that contains sections of text as objects inside of an array.

I’m trying to iterate over the initial array of sections, check to see what type the section is, and then based on the type iterate over the copy array to style and render each string as the appropriate HTML element.

The problem I’m running into is that I’m trying to accomplish this by using map and then a switch statement and then another map for each copy section and nothing is rendering.

Here’s a CodeSandbox I created as a mock up of the problem

This is my current component:

JavaScript

And this is the data structure:

JavaScript

Advertisement

Answer

Figured out what I was doing wrong and was able to display all elements correctly by pushing them to a second array within my function and returning that new array like so:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement