0

So I have the following:

// Build the component HTML.
return (
  <dl className={ classes }>

    {items.map((item, index) =>
      { item.type === 'dd' ?
        <dd key={ index } index={ index }>{ item.text }</dd>
        :
        <dt className="search-result__description-list__description" key={ index } index={ index }>{ item.text }</dt>
      }
    )}

  </dl>
);

The problem: nothing is rendering. The data is present in items. When I simply log the content without the if-else statement it also returns my data. However, when I use the if-else statement nothing shows up. No errors ether.

Any thoughts?

2 Answers 2

5

You forgot return statement

// Build the component HTML.
return (
  <dl className={ classes }>

    {items.map((item, index) =>
      { return item.type === 'dd' ?
        <dd key={ index } index={ index }>{ item.text }</dd>
        :
        <dt className="search-result__description-list__description" key={ index } index={ index }>{ item.text }</dt>
      }
    )}

  </dl>
);

You can see this fiddle with the same logic: https://jsfiddle.net/69z2wepo/94452/

Sign up to request clarification or add additional context in comments.

5 Comments

I need coffee >-< Thx for the prompt answer!
@WarreBuysse coffee can be really helpful in forgotten return keywords. To be specific: fat arrow function (which are borrowed from CoffeeScript). You can write fiddle from my answer without any return word! jsfiddle.net/yeg9et8w
Where did you learn to "speak" React so fluently? I'm working the basics here but I want/need to get more advanced in it. Any books/tutorials you can recommend? Or are you just a plain genious? ;-)
@WarreBuysse I don't now react (yet). Those topics aren't related with react, those are ES6 concepts. You can read about them in this great book: github.com/getify/You-Dont-Know-JS/blob/master/… Especially this chapter can be helpful for you: github.com/getify/You-Dont-Know-JS/blob/master/…
@WarreBuysse You can play more with mentioned fiddle even more (remove unnecessary brackets) jsfiddle.net/zu38wt6t
1

The problem is in your arrow function, when using the block syntax ({}s following the arrow), you will need to specify the returned value using the return keyword.

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.