当前位置:网站首页>扩展-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 /> </>
边栏推荐
- 定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见
- 2022 gas examination question bank and online simulation examination
- 嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
- 为什么香港服务器最适合海外建站使用
- [today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
- JMeter学习笔记2-图形界面简单介绍
- 基于Unet的环路滤波
- [recommended algorithm] C interview question of a small factory
- 【LeetCode】100. Same tree
- Do280 management application deployment --rc
猜你喜欢

【LeetCode】100. Same tree

25.k sets of flipped linked lists

Hololens2 development environment building and deploying apps

Maixll-Dock 使用方法

Class and object finalization

Annual inventory review of Alibaba cloud's observable practices in 2021

TASK04|数理统计

创新界,聚势行 | 2022人大金仓“百城巡展”火热开启
![[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached](/img/ec/96e3188902e399f536ebca79042af9.png)
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached

Coinbase in a bear market: losses, layoffs, stock price plunges
随机推荐
283. move zero
京东智能客服言犀意图体系搭建和意图识别技术介绍
类和对象收尾
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
674. longest continuous increasing sequence force buckle JS
Class and object finalization
Qt development experience tips 226-230
Valid @suppresswarnings warning name
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
这可能是你进腾讯最后的机会了..
NFT: start NFT royalty journey with eip-2981
[recommended algorithm] C interview question of a small factory
1. Mobile terminal touch screen event
"Target detection" + "visual understanding" realizes the understanding of the input image
Redis (VII) optimization suggestions
JS rotation chart
ThreeJS开篇
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
CF1638E colorful operations
[Master / slave] router election in DD message

