如果在地图返回中如何使用? [英] How to use if within a map return?

查看:53
本文介绍了如果在地图返回中如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于数据模型生成不同的 reactJS 代码,但我得到 <块引用>

在文件~/Scripts/Grid.jsx"中:解析错误:第 13 行:意外标记如果(在第 13 行第 15 列)行:52 列:3

使用此代码

var GridRow = React.createClass({渲染:函数(){变量行;row = this.props.cells.map(function(cell, i) {返回 (if(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));返回 (<tr>{排}</tr>);}});

渲染部分的使用方式似乎真的很有限?

解决方案

你把 return 语句放在 if 子句中,像这样:

 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));

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?

解决方案

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));

这篇关于如果在地图返回中如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆