React fragment shorthand failing to compile

前端 未结 3 927
渐次进展
渐次进展 2020-12-29 02:08

The project in question is using React-16.2.0 which has the capability to use Fragments and the Fragment shorthand.

https://reactjs.org/blog/2017/11/28/react-v16.2.0

相关标签:
3条回答
  • 2020-12-29 02:57

    You can use the poor man's fragment shorthand as a quick fix: [ , ]

    render(){
      return [
        <div>foo</div>,
        <div>bar</div>
      ]
    }
    

    as array is totally a valid jsx element.

    0 讨论(0)
  • 2020-12-29 03:04

    Fragment syntax is only supported by Babel v7.0.0-beta.31 & above.

    0 讨论(0)
  • 2020-12-29 03:06

    I think this is a reason:

    https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html#support-for-fragment-syntax

    screenshot

    create-react-apps currently use Babel 6.26.0 for full support React.Fragment is needed Babel v7.0.0-beta.31 and above

    ======================= EDIT

    It's working now with create-react-app v2 https://reactjs.org/blog/2018/10/01/create-react-app-v2.html

    0 讨论(0)
提交回复
热议问题