3

I have an array of objects with different artists. I want to show different content based on whether or not an image is in the individual object.

I have tried to put an if/else statement in my render method, but of course this is not working. Please see my code below.

render: function() {
    var cardList = this.props.artists.map(function(artist, index){
        return(
          <li key={index} className='card'>
            <span>  
                {if artist.image}
                   <img src="{artist.image}">
                {else}
                <p>{artist.message}</p>
                  <p>LIKES <i className="fa fa-heart" aria-hidden="true"></i> {artist.likeCount }</p>
              </span>
          </li>
        )
    })
    return (
      <div className='tickerwrapper'>
        <ul className='list'>{cardList}</ul>
      </div>
    )
  }

3 Answers 3

1

You can inline something along the lines of:

{ myVal === true ? <div>True</div> : <div>False</div> }

If, for example you only want something rendered if myVal is true you can do return null if it's false:

 { myVal === true ? <div>True</div> : null }

If the logic is quite large just extract it into a function and call that from within your render:

renderSalutation() {
  return (userLoggedOn ? <h3>Hello!</h3> : null);
}

render() {
  return (
    <div>
      { this.renderSalutation() }

      ------other renderings here

    </div>  
  );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Note that myVal === true can be abbreviated to a simple myVal.
Of course - left it like this for clarity as myVal is (hopefully obviously) made up.
I'm not sure how good of a practice it is, but sometimes I use the fact that render is a function to prepare pieces of view conditionnally as variables in its body, and then concat them in the return statement. THat way I avoid creating a function if its not goin gto be reused.
0

You need to use a valid expression in your JSX code. an if-else-statement is not, however you can use a ternary operator:

var cardList = this.props.artists.map(function(artist, index) {
    return(
        <li key={index} className='card'>
            <span>  
                {artist.image ? (
                    <img src="{artist.image}">
                ) : (
                    <p>{artist.message}</p>
                    <p>LIKES <i className="fa fa-heart" aria-hidden="true"></i> {artist.likeCount}</p>
                )}
            </span>
        </li>
    )
})

You can find more info on conditional rendering in the react docs.

2 Comments

can it be done elsewhere with an if/else statement also?
You could of course, e.g.: if(artist.image) { return (...) } else { return (...)}. Just make sure you keep the if-else-statement out of the JSX code. Often enough you loose some readability in the process, though.
0

It can be done with if/else. As shown on React site:

function NumberDescriber(props) {
  let description;

  if (props.number % 2 == 0) {
    description = <strong>even</strong>;
  } else {
    description = <i>odd</i>;
  }
  return <div>{props.number} is an {description} number</div>;
}

Just do something similar in your render function. Conditionally set a value to a component you want and then render it within {}.

Comments

Your Answer

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