Skip to content

Instantly share code, notes, and snippets.

@KoenBrouwer
Last active December 28, 2020 00:19
Show Gist options
  • Save KoenBrouwer/2f739c4a67bfcc7e1424a50a262c101f to your computer and use it in GitHub Desktop.
Save KoenBrouwer/2f739c4a67bfcc7e1424a50a262c101f to your computer and use it in GitHub Desktop.
Simple React component to condionally wrap something in JSX.
import React from "react";
interface ConditionalWrapProps {
condition: boolean;
wrap: (children: JSX.Element) => JSX.Element;
children: JSX.Element;
}
const ConditionalWrap: React.FC<ConditionalWrapProps> = ({condition, children, wrap}): JSX.Element =>
condition ? React.cloneElement(wrap(children)) : children;
export default ConditionalWrap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment