Skip to content

Instantly share code, notes, and snippets.

@gorangajic
Created December 22, 2016 11:57
Show Gist options
  • Save gorangajic/8c05a58c7a07b71af10a13a590edb2e3 to your computer and use it in GitHub Desktop.
Save gorangajic/8c05a58c7a07b71af10a13a590edb2e3 to your computer and use it in GitHub Desktop.
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