0

I'm new to React/Javascript and am trying to construct a JSX 'tree' using a loop to avoid hard-coding the data.

I guess my primary trouble lies with being unable to construct JSX piecemeal; Visual Studio Code wants the closing tag for my top-level opening JSX tag. In this case, . But since I'm building it in pieces, that's just not possible.

So I tried taking out of my logic and just having that in the render() function, with a separate function call in the middle of those tags. But that leaves me with...

index.js:2178 Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
    in div (created by Container)
    in Container (at RequestPlaceholders.js:45)
    in div (at RequestPlaceholders.js:41)
    in RequestPlaceholders (at App.js:80)
    in div (at App.js:79)
    in div (at App.js:65)
    in div (at App.js:64)
    in App (at index.js:8)

My code is here https://github.com/runelynx/WebServiceToolkit/blob/master/src/components/RequestPlaceholders/RequestPlaceholders.js if I'm not conveying my question accurately.

Can anyone point me in the right direction?

4
  • 1
    This line needs to be {this.renderPlaceholders()}
    – Varinder
    Commented Feb 19, 2018 at 21:00
  • 1
    not related to your error, but remember to add key to your Row component, e.g. <Row key={i}>
    – Kunukn
    Commented Feb 19, 2018 at 21:10
  • @Kunukn Just for my education... why?
    – runelynx
    Commented Feb 19, 2018 at 21:11
  • 1
    @runelynx reactjs.org/docs/lists-and-keys.html "Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity"
    – Kunukn
    Commented Feb 19, 2018 at 21:13

1 Answer 1

1

You need to call your renderPlaceholders function.

render() {
  return (
    <div>
      <h2 className="subtitle">
        {this.props.selectedAPI} Placeholders
      </h2>
      <Container>
        {this.renderPlaceholders()}
      </Container>
    </div>
  );
}

Also update your for loop you have an infinite bug. Change i+2 with i+=2

for (var i = 0; i < this.props.apiPlaceholders.length; i+=2)
2
  • Thanks, it flows now without error. But the moment Chrome enters into that loop logic, the app dies... eventually Chrome pauses processing due to an approaching out of memory issue. Does anything stand out in my loop as crash-worthy?
    – runelynx
    Commented Feb 19, 2018 at 21:07
  • 1
    @runelynx i've update the answer for the second issue
    – ztadic91
    Commented Feb 19, 2018 at 21:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.