Fragments should contain more than one child - otherwise, there’s no need for a Fragment at all
16 const data = results.data;
17
18 return (
19 <>20 <div>21 <div className="picture">22 <h2>Astromomy Picture Of The Day</h2>23 <figure>24 {data.media_type === "video" ? (25 <iframe src={data.url} title="a">26 {data.title}27 </iframe>28 ) : (29 <img src={data.url} alt={data.title} />30 )}31 <figcaption>32 {data.copyright && (33 <p>Image Credits and Copyright: {data.copyright}</p>34 )}35 <p className="title">36 {data.title} - {getReadableDate(data.date)}37 </p>38 </figcaption>39 </figure>40 </div>4142 <div className="explaination">43 <p>{data.explanation}</p>44 </div>45 </div>46 </>47 );
48};
49
Description
Fragments are a syntax that allow you to build a react component with multiple nodes or subcomponents, without requiring a wrapper element.
A fragment is considered to be redundant if:
- it contains only one child,
- it is the child of a html element,
- it is not a keyed fragment.
Removing the fragments can fix this issue.
Bad Practice
// Example 1
<>{foo}</>
// Example 2
<><Foo /></>
// Example 3
<p><>foo</></p>
// Example 4
<></>
// Example 5
<Fragment>foo</Fragment>
// Example 6
<React.Fragment>foo</React.Fragment>
// Example 7
<section>
<>
<div />
<div />
</>
</section>
Recommended
// Example 1
<>
<Foo />
<Bar />
</>
// Example 2
<>foo {bar}</>
// Example 3
<> {foo}</>
// Example 4
const cat = <>meow</>
// Example 5
<SomeComponent>
<>
<div />
<div />
</>
</SomeComponent>
// Example 6
<Fragment key={item.id}>{item.value}</Fragment>