Last active
April 21, 2023 13:56
-
-
Save Kolenov/6e8020d622de57d3439446899d9a7482 to your computer and use it in GitHub Desktop.
react
This file contains hidden or 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
// Conditional Rendering with enums | |
function Notification({ text, state }) { | |
return ( | |
<div> | |
{{ | |
info: <Info text={text} />, | |
warning: <Warning text={text} />, | |
error: <Error text={text} />, | |
}[state]} | |
</div> | |
); | |
} | |
// ---------------------------------- | |
const NOTIFICATION_STATES = { | |
info: <Info />, | |
warning: <Warning />, | |
error: <Error />, | |
}; | |
function Notification({ state }) { | |
return ( | |
<div> | |
{NOTIFICATION_STATES[state]} | |
</div> | |
); | |
} | |
// ----------------------------------------- | |
const getSpecificNotification = (text) => ({ | |
info: <Info text={text} />, | |
warning: <Warning text={text} />, | |
error: <Error text={text} />, | |
}); | |
function Notification({ state, text }) { | |
return ( | |
<div> | |
{getSpecificNotification(text)[state]} | |
</div> | |
); | |
} | |
//----------------------------------------------- | |
function FooBarOrFooOrBar({ isFoo, isBar }) { | |
const key = `${isFoo}-${isBar}`; | |
return ( | |
<div> | |
{{ | |
['true-true']: <FooBar />, | |
['true-false']: <Foo />, | |
['false-true']: <Bar />, | |
['false-false']: null, | |
}[key]} | |
</div> | |
); | |
} | |
FooBarOrFooOrBar.propTypes = { | |
isFoo: React.PropTypes.boolean.isRequired, | |
isBar: React.PropTypes.boolean.isRequired, | |
} | |
// HOC declaration | |
function withLoadingIndicator(Component) { | |
return function EnhancedComponent({ isLoading, ...props }) { | |
if (!isLoading) { | |
return <Component { ...props } />; | |
} | |
return <div><p>Loading...</p></div>; | |
}; | |
} | |
// Usage | |
const ListWithLoadingIndicator = withLoadingIndicator(List); | |
<ListWithLoadingIndicator | |
isLoading={props.isLoading} | |
list={props.list} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment