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> | |
) |
This comment has been minimized.
This comment has been minimized.
RedTn
commented
Nov 2, 2018
This fails if condition = false, and you are returning more than one child on React 15, if so I would change line 3 to
|
This comment has been minimized.
This comment has been minimized.
peacefulseeker
commented
Jan 2, 2019
•
or even using new React
|
This comment has been minimized.
This comment has been minimized.
Jianyi-Ren
commented
Sep 17, 2019
Thanks for sharing the unit test! How would you test the ConditionalWrap if it is defined within component render(){}? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
TheBox193 commentedOct 9, 2018
Some Unit tests: