Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 33.react-fragments.md #110

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions patterns/33.react-fragments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# React Fragments

React fragments are used whenever a component needs returned with multiple children.

Specifically, fragments are useful when I don't want to clutter the DOM with unnecessary `<div>` tags that are used purely to wrap children in a React render method.

For example, React fragments are commonly used to render list items:
```javascript
render() {
return (
<React.Fragment>
<td>Table Cell 1</td>
<td>Table Cell 2</td>
</React.Fragment>
);
}
```

This solves the issue of breaking the DOM HTML table specifications by not adding unnecessary `<div>` elements around `<td>` elements.
Do keep in mind, if it is a list being rendered, React does still throw warnings when children don't have the `key={}` prop.

### Related links:
- https://reactjs.org/docs/fragments.html