5 views
What is a React Fragment, and which props can it accept?
Asked at
AI answer
React.Fragment groups multiple children without adding an extra DOM node. This is useful, for example, when a component needs to return several adjacent elements:
return (
<React.Fragment>
<h2>Title</h2>
<p>Text</p>
</React.Fragment>
);
The shorthand syntax <>...</> is equivalent to Fragment, but does not allow props to be passed. To use a Fragment with a key, use the full syntax: <React.Fragment key={item.id}>...</React.Fragment>. key is useful, for example, when returning multiple elements from map; Fragment does not support arbitrary props. In special experimental/Canary builds of React, ref may also be available on Fragment.
Also asked at СБЕР
- Have you worked with useLayoutEffect and how does it differ from useEffect?
- [name] asked: why did you decide to change your job?
- What is Event Loop in JavaScript?
- What is Virtual DOM and how does it work in React?
- Please tell us about your experience: what tasks do you solve, what is your area of responsibility, what technologies have you worked with, and how are the team processes organized?
Similar questions
- What is React Fragment used for, and how does the full Fragment syntax differ from the shorthand <>...</>?
- What is JSX, and how does it work?
- If you replace the second element among three divs without keys with a span, what will React do: update the element or unmount it?
- Why is key needed in React, and what problems can arise when using an array index as the key?
- How can this React code be improved if children is recreated and causes a re-render?