Created
March 2, 2023 23:25
-
-
Save DWboutin/af967f5648ff2c53c331b042b708710e to your computer and use it in GitHub Desktop.
Reverse children order with React.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Children, FunctionComponent, ReactNode } from "react"; | |
export interface Props { | |
children: ReactNode; | |
reverse: boolean; | |
} | |
const ChildrenReversable: FunctionComponent<Props> = ({ children, reverse }) => { | |
const componentChildren = !reverse | |
? children | |
: Children.toArray(children).reverse(); | |
return <div>{Children.map(componentChildren, (child, i) => child)}</div>; | |
}; | |
export default ChildrenReversable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment