0

I am using ReactJS and a library called React-Table for an online gaming site.

In the table, I have one column cell that could potentially be empty or NULL.

So, if that column cell is null or empty or undefined, then I want to show a value called, "Dungeon Master".

If it's not null or empty, then I just want to show what should be shown(row.original.gamerTag).

So I tried using a ternary operator to check, but no matter what, the value always shows empty.

Here is where I use it:

{
    Header: 'Gamer Title',
    accessor: 'gamerTitle',
    Cell: ({ row }) =>
        <a href="#" onClick={() =>
            show(
                row.original.id,
                row.original.gamerTitle,
                row.original.gameType,
                (row.original.gamerTag == 'undefined' || '' || null) ? 'Dungeon Master' : row.original.gamerTag,
                row.original.gameDescription,
            )}>
            {row.original.gamerTitle}
        </a>
},

Am I using it wrong? I don't get any errors or anything.

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 2
    Does this answer your question? [JavaScript: Simple way to check if variable is equal to two or more values?](https://stackoverflow.com/questions/12116326/javascript-simple-way-to-check-if-variable-is-equal-to-two-or-more-values) – Heretic Monkey Jul 13 '20 at 15:38
  • 1
    In other words, that construct doesn't do what you think it does. – Heretic Monkey Jul 13 '20 at 15:39

2 Answers2

3

Replace

(row.original.gamerTag == 'undefined' || '' || null) ? 'Dungeon Master' : row.original.gamerTag

By

typeof row.original.gamerTag === 'undefined' || row.original.gamerTag === '' || row.original.gamerTag === null ? 'Dungeon Master' : row.original.gamerTag

Two problem, the one is myVar == 'undefined' doesnt work because you compare string and not type. And secondly, in js is not short syntaxe for concat condition. Alternatively you can try [undefined, null, ''].includes(row.original.gamerTag).

davidb
  • 111
  • 3
1

Try replacing:

(row.original.gamerTag == 'undefined' || '' || null) ? 'Dungeon Master' : row.original.gamerTag

with:

(row.original.gamerTag == 'undefined' || row.original.gamerTag == '' || row.original.gamerTag == null) ? 'Dungeon Master' : row.original.gamerTag
macborowy
  • 1,474
  • 10
  • 13