React片段速记无法编译 [英] React fragment shorthand failing to compile

查看:87
本文介绍了React片段速记无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题的项目正在使用React-16.2.0,它有能力使用Fragments和Fragment简写。

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-fragment-support.html

虽然全长语法工作正常...

While the full-length syntax works fine...

import React, { Fragment, Component } from 'react';

class TestingFragment extends Component {
    render() {
        return (
            <Fragment>
                <span>This is a fragment of text </span>
                <div>Another part of the fragment</div>
            </Fragment>
        )
    }
};

export default TestingFragment

速记无法编译,我在失去了为什么会这样。例如......

The shorthand fails to compile and I am at a loss as to why this is. Fore example...

import React, { Component } from 'react';

class TestingFragment extends Component {
    render() {
        return (
            <>
                <span>This is a fragment of text </span>
                <div>Another part of the fragment</div>
            </>
        )
    }
};

export default TestingFragment

无法编译如下...

Failed to compile
./src/testingFragments.js
Syntax error: Unexpected token (6:4)

  4 |   render() {
  5 |       return (
> 6 |           <>
    |            ^
  7 |               <span>This is a fragment of text </span>
  8 |               <div>Another part of the fragment</div>
  9 |           </>
This error occurred during the build time and cannot be dismissed.

这里有什么东西我缺少Fragment简写语法吗?

Is there something here I am missing about the Fragment shorthand syntax?

推荐答案

我认为这是一个原因:

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

屏幕截图

screenshot

create-react-apps目前使用 Babel 6.26.0
完全支持React.Fr需要agment Babel v7.0.0-beta.31 及以上

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

现在正在使用create-react-app v2
https://reactjs.org/blog/2018/10/01/create-react-app-v2。 HTML

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

这篇关于React片段速记无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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