博客
关于我
React 学习笔记 —— Fragment
阅读量:564 次
发布时间:2019-03-09

本文共 1071 字,大约阅读时间需要 3 分钟。

Handling Multiple Roots in React JSX

In React's JSX syntax, it's essential to use a single root tag. To avoid unnecessary extra div tags and hierarchical structures, we can use two main approaches.

Fragment Approach

The Fragment component is designed to be ignored in rendering. It allows embedding multiple root components without adding extra DOM nodes to the tree. Perfect for when you need to #[key] your fragments. If you have loops and dynamic content, specify the key attribute for each fragment is crucial.

Example:

import {Fragment} from 'react';...      test     test

Empty Tag Approach

Using empty tags is another great way to embed multiple root nodes without creating real DOM elements. They don’t accept any attributes since they’re meant purely for embedding HTML.

Example:

...   test   test

The key difference between Empty Tags and Fragments is that Fragments can only accept **key** attribute, which is important when you're dealing with list-like structures. However, Empty Tags have no such limitations and are often used for simple content embedding.

转载地址:http://ajxpz.baihongyu.com/

你可能感兴趣的文章
PostgreSQL配置文件--QUERY TUNING
查看>>
PostgreSQL配置文件--WAL
查看>>
PostgreSQL配置文件--其他
查看>>
PostgreSQL配置文件--复制
查看>>
PostgreSQL配置文件--实时统计
查看>>
PostgreSQL配置文件--资源使用(除WAL外)
查看>>
postgres访问认证配置文件pg_hba.conf
查看>>
Postgres间隔大量写IO的解决办法
查看>>
Posting JSON to Spring MVC Controller
查看>>
Qt开发——可隐藏的对话框
查看>>
Qt开发——QWebEngineView简易网络浏览器(二)
查看>>
Postman List类型传参方法
查看>>
Qt开发——QWebEngineView浏览器开发
查看>>
Postman —— 实现接口自动化测试!
查看>>
Qt开发——QStackedWidget嵌入式窗体切换
查看>>
Qt开发——QSplashScreen程序启动图
查看>>
Postman 内置的动态变量
查看>>
Qt开发——QProgressBar/QProgressDialog进度条
查看>>
Qt开发——QMouseEvent鼠标事件
查看>>
Postman 发送 post 请求,服务器接受到的却是 get 请求
查看>>