Ad

React - How Do I Include A Dynamic Variable With Ternary Operator

- 1 answer

In JSX, how do I include a dynamic variable with a ternary operator? The below JSX gives unexpected token error, however if I replace {x.title} with a literal say R it works.

let cardIDs = this.props.cards.map(x => <li key={x.title}>
{this.state.showName ? {x.title} : "S"}
</li>);
Ad

Answer

Remove {}

{ this.state.showName ? x.title : "S" }
                       ^^     ^^
Ad
source: stackoverflow.com
Ad