当前位置:网站首页>扩展-Fragment
扩展-Fragment
2022-07-01 04:26:00 【fang·up·ad】
背景
jsx语法要求return只能有一个根标签。App.js 用来组合其他组件,render方法的return一般需要一个div根标签,自编写的每个组件的render方法的return也都需要一个div根标签。这会导致div嵌套过多。
import React, { Component, Fragment } from 'react' import Demo from './04-fracment' export default class App extends Component { render() { return ( <div> <Demo /> </div> ) } }
解决办法
1.使用Fragment可以解决这个问题。
Fragment标签形式上是一个标签,但是reaxt解析时会自动去掉,类似于占位符。
每个return使用Fragment作为根标签,解析时react会去掉Fragment标签。
import React, { Component, Fragment } from 'react' import Demo from './04-fracment' export default class App extends Component { render() { return ( <Fragment> <Demo /> </Fragment> ) } }
此时,不再有div标签
2.使用空标签
效果同Fragment。区别主要还是Fragment标签可以加一个属性key。空标签不可以加。当需要遍历页面节点时,Fragment必须给个唯一的key值。
<> <Demo /> </>
边栏推荐
- Valid @suppresswarnings warning name
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
- Redis(七)优化建议
- js 图片路径转换base64格式
- [send email with error] 535 error:authentication failed
- 2022 t elevator repair question bank and simulation test
- [recommended algorithm] C interview question of a small factory
- NFT: utilisez EIP - 2981 pour commencer un voyage de redevances NFT
- Loop filtering based on Unet
- [leetcode skimming] February summary (updating)
猜你喜欢

软件研发的十大浪费:研发效能的另一面

DO280管理应用部署--RC

Ten wastes of software research and development: the other side of research and development efficiency

In the innovation community, the "100 cities Tour" of the gold warehouse of the National People's Congress of 2022 was launched

OdeInt与GPU

JS image path conversion Base64 format

2022 question bank and answers for safety production management personnel of hazardous chemical production units

Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services

尺取法:有效三角形的个数

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
随机推荐
Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
2022 polymerization process test questions and simulation test
一些小知识点
Learn Chapter 20 of vue3 (keep alive cache component)
尺取法:有效三角形的个数
定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
使用scroll-view实现滑块视图可能遇到的问题及其解决方法
JMeter learning notes 2 - brief introduction to graphical interface
互联网行业最佳产品开发流程 推荐!
Loop filtering based on Unet
离线安装wireshark2.6.10
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
2022 t elevator repair question bank and simulation test
How to ensure the idempotency of the high concurrency interface?
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
JMeter学习笔记2-图形界面简单介绍
LeetCode 1400. Construct K palindrome strings
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached

