Skip to content

Instantly share code, notes, and snippets.

@kitze
Created October 25, 2017 16:54
Show Gist options
  • Save kitze/23d82bb9eb0baabfd03a6a720b1d637f to your computer and use it in GitHub Desktop.
Save kitze/23d82bb9eb0baabfd03a6a720b1d637f to your computer and use it in GitHub Desktop.
one-line React component for conditionally wrapping children
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>
)
@badalsaibo
Copy link

FYI this function breaks the react/no-unstable-nested-components eslint rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment