Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gregberge/react-flatten-children
React utility to flatten fragments 🗜
https://github.com/gregberge/react-flatten-children
children flatten fragment react utility
Last synced: 5 days ago
JSON representation
React utility to flatten fragments 🗜
- Host: GitHub
- URL: https://github.com/gregberge/react-flatten-children
- Owner: gregberge
- License: mit
- Created: 2018-10-01T19:56:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T14:28:33.000Z (over 4 years ago)
- Last Synced: 2024-10-18T08:08:40.136Z (19 days ago)
- Topics: children, flatten, fragment, react, utility
- Language: TypeScript
- Size: 419 KB
- Stars: 59
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# react-flatten-children
[![License](https://img.shields.io/npm/l/react-flatten-children.svg)](https://github.com/gregberge/react-flatten-children/blob/master/LICENSE)
[![npm package](https://img.shields.io/npm/v/react-flatten-children/latest.svg)](https://www.npmjs.com/package/react-flatten-children)
[![Build Status](https://img.shields.io/travis/gregberge/react-flatten-children.svg)](https://travis-ci.org/gregberge/react-flatten-children)
[![DevDependencies](https://img.shields.io/david/dev/gregberge/react-flatten-children.svg)](https://david-dm.org/gregberge/react-flatten-children?type=dev)React utility to flatten fragments 🗜
```sh
npm install react-flatten-children
```## Example
```js
import React from "react";
import { Switch as BaseSwitch } from "react-router";
import flattenChildren from "react-flatten-children";
import PublicHome from "./PublicHome";
import PrivateHome from "./PrivateHome";
import Account from "./Account";
import Login from "./Login";// Create a fragment ready Switch
const Switch = ({ children }) => (
{flattenChildren(children)}
);const Routes = ({ isLoggedIn }) => (
{isLoggedIn ? (
<>
>
) : (
<>
>
)}
);export default Routes;
```👉 [Checkout an interactive example on CodeSandbox](https://codesandbox.io/s/nn6l3r30k0)
## Why?
In many cases you have to introspect children, it can be to [use the first route matching a path](https://reacttraining.com/react-router/web/api/Switch), extract the label of a tab, or another use case.
React considers fragments as children, even if it is in fact a group of children. This package flattens children and makes your component API compatible with fragments. Users expect your library to be compatible with fragments. If you want to avoid tons of issues (see https://github.com/ReactTraining/react-router/issues/5917, https://github.com/ReactTraining/react-router/issues/5785), you should use it!
# License
MIT