React Fragment может принимать только один атрибут — key:
// С атрибутом key
<React.Fragment key={item.id}>
<td>{item.name}</td>
<td>{item.value}</td>
</React.Fragment>
// Короткий синтаксис (без атрибутов)
<>
<h1>Title</h1>
<p>Content</p>
</>React Fragment — это специальный компонент для группировки элементов без создания дополнительного DOM-узла! Он принимает только один атрибут — key. 🎯
Fragment может принимать только атрибут key:
// ✅ Правильно - только key
<React.Fragment key="unique-key">
<h1>Header</h1>
<p>Paragraph</p>
</React.Fragment>
// ❌ Неправильно - другие атрибуты не поддерживаются
<React.Fragment className="container" id="wrapper">
<div>Content</div>
</React.Fragment>import React from 'react';
function ComponentWithKey() {
const items = [
{ id: 1, name: 'Item 1', value: 'Value 1' },
{ id: 2, name: 'Item 2', value: 'Value 2' }
];
return (
<table>
<tbody>
{items.map(item => (
<React.Fragment key={item.id}>
<tr>
<td>{item.name}</td>
<td>{item.value}</td>
</tr>
<tr>
<td colSpan="2">Description for {item.name}</td>
</tr>
</React.Fragment>
))}
</tbody>
</table>
);
}function ShortSyntax() {
return (
<>
<h1>Title</h1>
<p>This is a paragraph</p>
<button>Click me</button>
</>
);
}function TableRows({ data }) {
return (
<table>
<tbody>
{data.map(item => (
<React.Fragment key={item.id}>
<tr>
<td>{item.title}</td>
<td>{item.status}</td>
</tr>
{item.showDetails && (
<tr>
<td colSpan="2">{item.details}</td>
</tr>
)}
</React.Fragment>
))}
</tbody>
</table>
);
}function ConditionalFragments({ items }) {
return (
<div>
{items.map(item => (
<React.Fragment key={item.id}>
<h3>{item.title}</h3>
{item.hasSubtitle && <h4>{item.subtitle}</h4>}
<p>{item.content}</p>
{item.showDivider && <hr />}
</React.Fragment>
))}
</div>
);
}function FormFields({ showAdvanced }) {
return (
<form>
<>
<input type="text" placeholder="Name" />
<input type="email" placeholder="Email" />
</>
{showAdvanced && (
<>
<input type="tel" placeholder="Phone" />
<textarea placeholder="Comments" />
</>
)}
<button type="submit">Submit</button>
</form>
);
}function UserProfile({ user, isOwner }) {
return (
<div className="profile">
<img src={user.avatar} alt="Avatar" />
<h2>{user.name}</h2>
{isOwner && (
<>
<button>Edit Profile</button>
<button>Settings</button>
<button>Logout</button>
</>
)}
</div>
);
}function withLoading(WrappedComponent) {
return function WithLoadingComponent({ isLoading, ...props }) {
if (isLoading) {
return (
<>
<div className="spinner" />
<p>Loading...</p>
</>
);
}
return <WrappedComponent {...props} />;
};
}| Синтаксис | Атрибуты | Использование |
|---|---|---|
<React.Fragment> | Поддерживает key | Когда нужен key |
<>...</> | Не поддерживает | Простая группировка |
// Когда key НЕ нужен - используйте короткий синтаксис
function SimpleGroup() {
return (
<>
<h1>Header</h1>
<p>Content</p>
</>
);
}
// Когда key нужен - используйте полный синтаксис
function ListWithFragments({ items }) {
return (
<div>
{items.map(item => (
<React.Fragment key={item.id}>
<h3>{item.title}</h3>
<p>{item.description}</p>
</React.Fragment>
))}
</div>
);
}// Создаёт дополнительный div в DOM
function WithDiv() {
return (
<div>
<h1>Title</h1>
<p>Content</p>
</div>
);
}
// Не создаёт дополнительный узел
function WithFragment() {
return (
<>
<h1>Title</h1>
<p>Content</p>
</>
);
}// Массив - каждому элементу нужен key
function ArrayElements() {
return [
<h1 key="title">Title</h1>,
<p key="content">Content</p>
];
}
// Fragment - key не нужен
function FragmentElements() {
return (
<>
<h1>Title</h1>
<p>Content</p>
</>
);
}❌ Неправильно:
// Попытка добавить другие атрибуты
<React.Fragment className="wrapper">
<div>Content</div>
</React.Fragment>
// Использование короткого синтаксиса с key
<key={item.id}>
<div>{item.name}</div>
</>✅ Правильно:
// Только key атрибут
<React.Fragment key={item.id}>
<div>{item.name}</div>
</React.Fragment>
// Короткий синтаксис без атрибутов
<>
<div>Content</div>
</>React Fragment атрибуты:
Используйте Fragment для чистой разметки! 🎯