JSX elements with no children must be self-closing

江枫思渺然 提交于 2020-03-03 12:27:12

问题


I can run this code correctly. But when I commit my code to git, it shows 'ERROR: src/layouts/index.tsx:25:9 - JSX elements with no children must be self-closing'.

I have tried to add React.Fragment tag. But it doesn't work. I also delete line 25. But the same error shows. Still 'index.tsx:25:9'. I also have tried to use to be self-closing. But I have to put value into the Menu.Item.

Here is my code:

import * as React from 'React';
import {Menu, Icon} from 'antd';
import './test.less';
import styles from './index.css';

const SubMenu = Menu.SubMenu;
class LeftBar extends React.Component{
  constructor(props: any) {
    super(props);
  }
  hideLeftBar = () => {
    // console.log(styles);
  }
  public render() {
    return (
      <div className={styles.leftbar}>
        <Menu
          className={styles.menu}
          mode="inline"
          // openKeys={this.state.openKeys}
          // onOpenChange={this.onOpenChange}
          style={{ width: 250 }}
        >
          <SubMenu className="test" key="sub1" title={<span><Icon type="mail" /><span>Navigation One</span></span>}>
            <Menu.Item key="2">Option 2</Menu.Item>
            <Menu.Item key="3">Option 3</Menu.Item>
            <Menu.Item key="4">Option 4</Menu.Item>
          </SubMenu>
          <SubMenu key="sub2" title={<span><Icon type="appstore" /><span>Navigation Two</span></span>}>
            <Menu.Item key="5">Option 5</Menu.Item>
            <Menu.Item key="6">Option 6</Menu.Item>
            <SubMenu key="sub3" title="Submenu">
              <Menu.Item key="7">Option 7</Menu.Item>
              <Menu.Item key="8">Option 8</Menu.Item>
            </SubMenu>
          </SubMenu>
          <SubMenu key="sub4" title={<span><Icon type="setting" /><span>Navigation Three</span></span>}>
            <Menu.Item key="9">Option 9</Menu.Item>
            <Menu.Item key="10">Option 10</Menu.Item>
            <Menu.Item key="11">Option 11</Menu.Item>
            <Menu.Item key="12">Option 12</Menu.Item>
          </SubMenu>
          <div onClick={this.hideLeftBar} className={styles.hide}/>
        </Menu>
      </div>
    );
  }
}

export default LeftBar;

Hoping that I can commit successfully and push it to my remote repository.


回答1:


This error just means instead of e.g.:

<div></div>

you should have:

<div />

The latter is self-closing.



来源:https://stackoverflow.com/questions/55652887/jsx-elements-with-no-children-must-be-self-closing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!