在 React 16.2 中出现意外令牌错误的片段 [英] Fragments giving Unexpected token error in React 16.2

查看:49
本文介绍了在 React 16.2 中出现意外令牌错误的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下组件来呈现一系列组件.但是,我下载了 React 16.2 并尝试使用片段而不是 div,但出现以下错误:

I have the following component that renders a series of components. However, I downloaded React 16.2 and tried to use fragments instead of divs, but I get the following error:

Error in ./src/containers/answers.js
Syntax error: Unexpected token (24:5)

  22 |     
  23 | return (
> 24 |     <>
     |      ^
  25 |       {AnswersCard}
  26 |     </>
  27 |    )

为什么在 React 16.2 中片段应该能够替换 div 时我会收到此错误?

Why am I getting this error when fragments are supposed to be able to replace divs in React 16.2?

  question ? 
    AnswersCard = ( question.answers.sort(function(a,b) { return (a.count < b.count) ? 1 : ((b.count > a.count) ? -1 : 0)} 
    ).map(answer =>  
    <Answer key={answer.id} answer={answer} questionId={question.id} />
    )) : AnswersCard = ( <p>Loading...</p> )

return (
    <>
      {AnswersCard}
    </>
   )
  }
}

推荐答案

根据 文档,语法 <></> 并非所有工具都支持,它们鼓励您使用 代替

As per the documentation, the syntax <></> is not supported by all tools and they encourage you to use <React.Fragment> instead

检查 this 关于支持 Fragment 语法的文档

Check this documentation on Support for Fragment syntax

这篇关于在 React 16.2 中出现意外令牌错误的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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