react:React.Fragment的使用

官方文档

介绍

相当于一个空标签,同 vue 的 template 相似

使用

class Columns extends React.Component {
    
    
  render() {
    
    
    return (
      <React.Fragment>
        <td>Hello</td>
        <td>World</td>
      </React.Fragment>
    );
  }
}

class Columns extends React.Component {
    
    
  render() {
    
    
    return (
      <>
        <td>Hello</td>
        <td>World</td>
      </>
    );
  }
}

注意

React.Fragment 写法可以带 key,<></> 不能带 key

function Glossary(props) {
    
    
  return (
    <dl>
      {
    
    props.items.map(item => (
        // 没有`key`,React 会发出一个关键警告
        <React.Fragment key={
    
    item.id}>
          <dt>{
    
    item.term}</dt>
          <dd>{
    
    item.description}</dd>
        </React.Fragment>
      ))}
    </dl>
  );
}

猜你喜欢

转载自blog.csdn.net/weixin_43972437/article/details/115037859