Created
December 22, 2016 11:57
-
-
Save gorangajic/8c05a58c7a07b71af10a13a590edb2e3 to your computer and use it in GitHub Desktop.
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 Hit = ({ component: Component }) => <Component />; | |
const Miss = ({ component: Component }) => <Component />; | |
Hit.propTypes = Miss.propTypes = { | |
component: React.PropTypes.any, | |
}; | |
const HitMiss = ({ children }) => { | |
let component = null; | |
let miss = null; | |
React.Children.forEach(children, (child) => { | |
if (component) { | |
return; | |
} | |
if (child.type === Miss) { | |
miss = child; | |
return; | |
} | |
if (child.props.check) { | |
component = child; | |
} | |
}); | |
return component || miss; | |
}; | |
HitMiss.propTypes = { | |
children: React.PropTypes.node, | |
}; | |
export default HitMiss; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment