react · React Basics
___ can be done while multiple elements need to be returned from a component.
Answers
- Abstraction
- Packing
- Insulation
- Wrapping
{/*Wrapping div*/}
);
}
```
In the above example, the function component `App` returns two elements: an `h1` element and a `p` element. But as JSX requires them to be wrapped in a single parent element, these two are enclosed within a `div` wrapper.
As best practice, it is always recommended to keep components granular, each serving its own purpose. This makes the code more readable and maintainable. Wrapping is invaluable in providing this structure, allowing developers to form neatly arranged return statements while adhering to JSX's requirements.
With the introduction of React 16.2.0, a shorthand syntax for wrapping was introduced called 'Fragment'. Using Fragment, the earlier example can be rewritten as:
```jsx
import React, { Fragment } from 'react';
function App() {
return (
Hello World
Welcome to the application!
Hello World
Welcome to the application!