Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active June 10, 2022 09:00
Show Gist options
  • Save stekhn/fcf868132c683015ff56a3b0b278cb84 to your computer and use it in GitHub Desktop.
Save stekhn/fcf868132c683015ff56a3b0b278cb84 to your computer and use it in GitHub Desktop.
React conditional wrapper that add an enclosing element based on a boolean condition
import * as React from 'react';
interface Props {
condition: boolean;
wrapper: Function;
children: React.ReactNode;
}
export const ConditionalWrapper: React.FC<Props> = ({ condition, wrapper, children }) => {
return condition ? wrapper(children) : children;
};
// Usage
// <ConditionalWrapper
// condition={width < 600}
// wrapper={(children: React.ReactNode) => (
// <SomeWrapper>
// {children}
// </SomeWrapper>
// )}
// >
// <h1>Content that should be conditionally wrapper</h1>
// </ConditionalWrapper>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment