Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary Conditions in React Components className

I am pulling data from a json file and trying to add a class only when a specific condition is met:

    render: function() {
    var gameList = this.props.data.map(function(game) {
        return (
            <li key={game.id} className="list-group-item">
                    {game.home_team_name}
                    <span className="pull-right {game.home.score} > {game.away.score} ? 'highlight':'' ">{game.home.score}</span> <br />
                    {game.away_team_name}
                    <span className="pull-right {game.home.score} > {game.away.score} ? 'highlight':'' ">{game.away.score}</span> <br />
                    {game.status.status}
            </li>   
        );
    });

This method seems to only add whatever is there into the class and not the "highlight" class if the condition is met.

Also is it possible to define a variable again inside the .map method to reduce repetition?

I've tried var homeScore = {game.home.score} but, with no luck

like image 693
Simon Avatar asked Feb 16 '16 01:02

Simon


People also ask

How use ternary operator in className React?

To set className with ternary operator add class 'null' in React, we can set the class name to an empty string. We set className to 'on' if on is true and an empty string otherwise. And we set the the on class to have background color green.

How do you apply condition on className in React?

The simplest approach is to use a ternary statement inside of a template literal. In this example, banner is always on the div , but active depends on the active prop. If you have a lot of class names that need to be conditional on the same element, it can get a little messy, but it still works.

How use conditional CSS React?

Create a new react application or open existing react app. Declare two different CSS style objects named as objectStyle and objectStyleValid with below attributes (in Code). Next we will declare a const variable named “isValid”. Based on its value (true/false) we will try to change the CSS style.


1 Answers

You need to put the whole ternary statement together into brackets :

{parseInt(game.home.score, 10) > parseInt(game.away.score, 10) ? 'highlight':''}

And yes, you can define another variable. Just put it before return:

render: function() {
    var gameList = this.props.data.map(function(game) {
        var classNameExt = parseInt(game.home.score, 10) > parseInt(game.away.score, 10) ? 'highlight':'';
        return (
            <li key={game.id} className="list-group-item">
                    {game.home_team_name}
                    <span className={'pull-right ' + classNameExt}>{game.home.score}</span> <br />
                    {game.away_team_name}
                    <span className={'pull-right ' + classNameExt}>{game.away.score}</span> <br />
                    {game.status.status}
            </li>   
        );
    });

Or to highlight only the winner:

render: function() {
    var gameList = this.props.data.map(function(game) {
        return (
            <li key={game.id} className="list-group-item">
                    {game.home_team_name}
                    <span className={'pull-right ' + (parseInt(game.home.score, 10) > parseInt(game.away.score, 10) ? 'highlight':'')}>{game.home.score}</span> <br />
                    {game.away_team_name}
                    <span className={'pull-right ' + (parseInt(game.home.score, 10) < parseInt(game.away.score, 10) ? 'highlight':'')}>{game.away.score}</span> <br />
                    {game.status.status}
            </li>   
        );
    });
like image 125
baao Avatar answered Oct 01 '22 22:10

baao