Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created March 2, 2023 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DWboutin/af967f5648ff2c53c331b042b708710e to your computer and use it in GitHub Desktop.
Save DWboutin/af967f5648ff2c53c331b042b708710e to your computer and use it in GitHub Desktop.
Reverse children order with React.js
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