Skip to content

Instantly share code, notes, and snippets.

@johnborges
Last active August 28, 2019 01:43
Show Gist options
  • Save johnborges/dfcdb1914fc269751a490593664c521c to your computer and use it in GitHub Desktop.
Save johnborges/dfcdb1914fc269751a490593664c521c to your computer and use it in GitHub Desktop.
Logic-Less JSX. Keep the JSX of your components clean by putting data logic in variables.
const Animal = ({ id, name, legCount, isFriendly }) => {
const url = `url/animal/${id}`
const legCountStr = toString(legCount) || '?'
const friendliness = { true: 'Friendly', false: 'Unfriendly' }[isFriendly]
const hasNotEnoughData = legCount === undefined && isFriendly === undefined
return (
<li>
<a href={url}>{name}</a> - {legCountStr} legs
{friendliness && ` - ${friendliness}`}
{hasNotEnoughData && ' - Not enough data!'}
</li>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment