reactjs - React 16.2 中的片段给出意外的 token 错误

标签 reactjs react-16

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

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

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

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

  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}
    </>
   )
  }
}

最佳答案

根据 documentation ,语法<></>并非所有工具都支持,它们鼓励您使用 <React.Fragment>相反

检查this 有关片段语法支持的文档

关于reactjs - React 16.2 中的片段给出意外的 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596157/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'filter' of undefined

reactjs - React Router 与 React 16.6 Suspense "Invalid prop ` 组件`类型为 `object` 提供给 `Route` ,预期为 `function` 。”

javascript - 向 FlatList 添加标题

javascript - 触发器与 ref react dropzone 不起作用

javascript - 未捕获的类型错误 : Cannot read property 'injection' of undefined

reactjs - React 16.3.*上下文提供者错误: Expected string or class/func but got: Object

reactjs - 在错误边界中包装多个React-Router路由

javascript - React Fragment 未在函数中呈现

reactjs - 检测 React Web App Safari 中的方向变化

reactjs - Webpack 编译文件到 S3 或 CDN 服务器