41

I need to generate diffrent reactJS code based on datamodel but I get

In file "~/Scripts/Grid.jsx": Parse Error: Line 13: Unexpected token if (at line 13 column 15) Line: 52 Column:3

With this code

var GridRow = React.createClass({
    render: function() {
        var row;

        row = this.props.cells.map(function(cell, i) {
            return (
                if(cell.URL != null && cell.URL.length > 0){
                    <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>        
                }
                else {
                    <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>
                }
            );
        }.bind(this));

        return (
            <tr>
                {row}
            </tr>
        );
    }
});

The render part seems to be really limited in how it can be used?

Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Have you tried defining each branch and determining which is required _before_ the return statement? – pdoherty926 Feb 17 '15 at 17:41
  • I'm not quite pro at all with render js, but it seems to me that it doesn't fully rewrite javascript: an if statement may not be found within a return statement – axelduch Feb 17 '15 at 17:41
  • It appears that in both cases your if-else creates an identical . – tengbretson Feb 17 '15 at 20:21

2 Answers2

49

You put return statement inside if clause like so:

    row = this.props.cells.map(function(cell, i) {

        if(cell.URL != null && cell.URL.length > 0){
            return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;        
        }
        else {
            return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;
        }

    }.bind(this));
nilgun
  • 10,460
  • 4
  • 46
  • 57
11

You could also use a ternary (inline if/else) statement. It might look like this:

row = this.props.cells.map(function(cell, i) {
    return (cell.URL != null && cell.URL.length > 0) ? 
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>) :
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>)
}.bind(this));

or es6

row = this.props.cells.map((cell, i) => (cell.URL != null && cell.URL.length > 0) ? 
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>) :
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>)
);

but, for readability, I would suggest nilgun's answer.

Although I would remove the else statement, since it is redundant. You could also remove the curly brackets, this is a matter of preference.

row = this.props.cells.map(function(cell, i) {
    if(cell.URL != null && cell.URL.length > 0)
        return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;        
    return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;
}.bind(this));
S.Kiers
  • 4,086
  • 3
  • 32
  • 38
  • Hi @S.Kiers, on the e6 example, how do we do an else if? – Malcolm Salvador May 07 '20 at 19:28
  • 1
    You are talking about the ternary example? If you need else if, I suggest you use a full if/elseif/else block. Ternary operator is usually just syntactic sugar. However, if you need to you can 'nest' ternary operators ( example, in react you *could* do this, but don't!): `condition1 ? "This is the IF" : condition2 ? "This is the ELSE IF" : "This is the ELSE"` – S.Kiers May 09 '20 at 17:54