Last active
June 10, 2022 09:00
-
-
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
This file contains 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 * 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