Created
October 25, 2017 16:54
-
-
Save kitze/23d82bb9eb0baabfd03a6a720b1d637f to your computer and use it in GitHub Desktop.
one-line React component for conditionally wrapping children
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 React from 'react'; | |
const ConditionalWrap = ({condition, wrap, children}) => condition ? wrap(children) : children; | |
const Header = ({shouldLinkToHome}) => ( | |
<div> | |
<ConditionalWrap | |
condition={shouldLinkToHome} | |
wrap={children => <a href="/">{children}</a>} | |
> | |
<img src="logo.png"/> | |
</ConditionalWrap> | |
</div> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an improvement suggestion for this:
By using
createElement
we can use Wrap like so:Instead of always passing a
(children) => <jsx />
function.