简体   繁体   English

reactjs意外的令牌“ /”

[英]reactjs Unexpected token “/”

Hello i am trying to render a React component dynamically look: 您好,我正在尝试呈现一个React组件动态外观:

render: function() {
    return (
        <form  {...this.props}>
            <div className={'row'}>
                {this.props.fields.map(function(field, index){
                    if (index != 0 &&index%3==0) {
                        return (
                            </div><div className={'row'}>
                            <div className={'col-sm-4'}>
                                ...
                            </div>
                        )
                    } else{
                        return (
                            <div className={'col-sm-4'}>
                                ...
                            </div>
                        )
                    }
                })}
            </div>
        </form>
    );
}

but i am getting this error "Parse Error: Line 17: Unexpected token / while parsing file..." 但我收到此错误“解析错误:第17行:意外的令牌/解析文件...”

the line 17 in my file is </div><div className={'row'}> 我文件中的第17行是</div><div className={'row'}>

There is another way to do this? 还有另一种方法吗? and is it possible to put all my duplicated code inside a variable? 是否可以将我所有重复的代码放在一个变量中?

Obs: i am using browserify(7.0.x) and reactfy(0.17.1) Obs:我正在使用browserify(7.0.x)和reactfy(0.17.1)

To formalize the correct answer described by FakeRainBrigand and elaborated on by dirkk in the comments above, here's a modified render that fixes the invalid HTML: 为了使FakeRainBrigand描述的正确答案正式化,并在dirkk在上面的评论中详细阐述,下面是修改后的render ,它修复了无效的HTML:

render: function() {
  return (
    <form  {...this.props}>
      <div className={'row'}>
        {
          this.props.fields.map(function(field, index) {
            if (index !== 0 && index % 3 === 0) {
              return (
                <div className={'row'}>
                  <div className={'col-sm-4'}>
                    {/* Whatever else each field is supposed to do if the index isn't zero but is divisible by three */}
                  </div>
                </div>
              )
            } else {
              return (
                <div className={'col-sm-4'}>
                  {/* Whatever else each field is supposed to do if the index is zero or isn't divisible by three */}
                </div>
              )
            }
          })
        }
      </div>
    </form>
  );
}

The issue was in the if block, which (with minor code formatting updates) initially looked like this: 问题出在if块中,该块(带有较小的代码格式更新)最初看起来像这样:

if (index !== 0 && index % 3 === 0) {
  return (
    </div>
    <div className={'row'}>
      <div className={'col-sm-4'}>
        {/* ... */}
      </div>
  )
}

The </div> at the start of the block is invalid HTML since it is not associated with an earlier <div> --you can't close a tag that hasn't been opened. 块开头的</div>是无效的HTML,因为它与早期的<div>没有关联-您无法关闭尚未打开的标签。 However, removing the leading </div> won't solve the issue: by fixing the indentation it becomes clear that the </div> was likely intended to be at the end of this block, as the div with class row is never closed. 但是,删除开头的</div>并不能解决问题:通过修复缩进,可以清楚地知道</div>可能位于此块的末尾,因为带有类rowdiv永远不会关闭。 The corrected version of this block looks like this: 该块的更正版本如下所示:

if (index !== 0 && index % 3 === 0) {
  return (
    <div className={'row'}>
      <div className={'col-sm-4'}>
        {/* Whatever else each field is supposed to do if the index isn't zero but is divisible by three */}
      </div>
    </div>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM