当前位置:网站首页>扩展-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 /> </>
边栏推荐
- Daily question - line 10
- What are permissions? What are roles? What are users?
- 【无标题】
- slf4j 简单实现
- Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
- Qt开发经验小技巧226-230
- Analysis and case of pageobject mode
- 2022年聚合工艺考试题及模拟考试
- TASK04|數理統計
- After many job hopping, the monthly salary is equal to the annual salary of old colleagues
猜你喜欢

Loop filtering based on Unet

Recommend the best product development process in the Internet industry!

离线安装wireshark2.6.10

Go learning --- unit test subtest
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

2022年化工自动化控制仪表操作证考试题库及答案

MallBook:后疫情时代下,酒店企业如何破局?

TASK04|数理统计

Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
随机推荐
2022 t elevator repair question bank and simulation test
Valid @suppresswarnings warning name
Possible problems and solutions of using scroll view to implement slider view
Use winmtr software to simply analyze, track and detect network routing
Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
LeetCode 1400. Construct K palindrome strings
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
[learn C and fly] S1E20: two dimensional array
TS type gymnastics: illustrating a complex advanced type
Obtain detailed ideas for ABCDEF questions of 2022 American Games
Browser top loading (from Zhihu)
使用scroll-view实现滑块视图可能遇到的问题及其解决方法
Unity's 3D multi-point arrow navigation
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
尺取法:有效三角形的个数
Maixll-Dock 快速上手
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
674. longest continuous increasing sequence force buckle JS
高并发下接口幂等性如何保证?
Grey correlation cases and codes

