Skip to content

Instantly share code, notes, and snippets.

@behnamazimi
Created March 4, 2019 07:55
Show Gist options
  • Save behnamazimi/7c26f4457a535b989a6fa241efe60c9d to your computer and use it in GitHub Desktop.
Save behnamazimi/7c26f4457a535b989a6fa241efe60c9d to your computer and use it in GitHub Desktop.
Code DRY
// Code DRY
// Dirty Code
export default App = (props) => {
return (
<div className="my-app">
<div className="custom-button">
<span className="icon icon-user"/>
<span className="content">Add New Member</span>
</div>
<div className="custom-button">
<span className="icon icon-report"/>
<span className="content">Report User</span>
</div>
<div className="custom-button">
<span className="icon icon-times"/>
<span className="content">Close Chat</span>
</div>
<div className="custom-button">
<span className="icon icon-share"/>
<span className="content">Share Chat Link</span>
</div>
</div>
)
}
// Clean Code
// You must create your custom button component and use it where you want
const CustomButton = ({iconName, content = '', children}) => {
return (
<div className="custom-button">
{iconName &&
<span className={`icon icon-${iconName}`}/>}
{content &&
<span className="content">{content}</span>}
{/* render children of this component if content be empty/null */}
{!content && children}
</div>
)
}
export default App = (props) => {
return (
<div className="my-app">
<CustomButton content="Add New Member" iconName="user"/>
<CustomButton content="Report User" iconName="report"/>
<CustomButton content="Close Chat" iconName="report"/>
<CustomButton>
<span className="icon icon-share"/>
<span className="content">Share Chat Link</span>
</CustomButton>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment