新建tsx文件编写布局代码时报错

Cannot use JSX unless the '--jsx' flag is provided

错误发生在下面代码的div上,是因为tsconfig.json里没有添加jsx的配置。

import React from 'react'

export default class demo extends React.Component {

  render() {
    return (
      <div>
        Hello
      </div>
    );
  }
}

解决办法:

tsconfig.json中加入:

"jsx": "react",

如果保存之后仍然报错,需重启VSCode生效。