React Fragment in React Js
- Get link
- X
- Other Apps
React Fragment in React Js
It was first introduced in react version 16.2
Fragments lets you to group a list of children without adding extra nodes ( div ) to the DOM.
It is common practice in react Js that, we need to return multiple elements from a component.
Sample Code:
A) App.js ( with div tag ) - There is a one problem associated with below code, clearly it will add one extra div tag which is unnecessary.
B) App.js ( with <React.Fragment> tag ) - This will not add any extra element in DOM, so its solution to above problem.
C) App.js ( with Empty tag <> ) - We can use empty tag ( <> ) as alternative to ‘<React.Fragment>’ but important thing to remember here is that empty tag don’t have key attribute so it will be not useful for iteration through a list or a collection (Refer Keyed Fragments below ).
Keyed Fragments:
<React.Fragment> syntax may have key attribute. This will be useful while iterating through a list or a collection as shown below.
Benefits of React Fragment:
1) a) It takes less memory.
2)
b) As div tag is removed while rendering, execution
of code is faster.
Happy Learning!! Happy Coding!!
- Get link
- X
- Other Apps
Comments
New comments are not allowed.
Great Post! Robtechworld which is a Web Designer In Chandigarh provides unique and unparalleled web-related services.
ReplyDeleteThanks for sharing this Great Article. This Content is very useful Information.
ReplyDeleteDevOps Online Training in Hyderabad
DevOps Online Training institute
DevOps Training Online
DevOps Online Course