Created
December 15, 2015 23:47
-
-
Save jkup/b2f5e1d742cfd05b6552 to your computer and use it in GitHub Desktop.
React Question
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
<div className={(condition) ? 'grid-item Rejection Rejection--width2' : 'grid-item Rejection'}> | |
// How can I do this without repeating the grid-item and Rejection classes? | |
// Something like: | |
//<div className={"grid-item Rejection" if (condition) { "Rejection--width2" }}> |
Using classnames:
var classNames = require('classnames')
<div className={classNames('grid-item Rejection', condition && 'Rejection--width2')}>
+1 for the classnames library. It may seem like a lot to solve just one problem but once you get used to it you'll use it all over the place. It completely eliminates the need for dirty string concatenation.
@insin, it's more idiomatic to use an object to hold conditions:
<div className={classNames('grid-item Rejection', { 'Rejection--width2' : condition })}>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<div className={'grid-item Rejection ' + (condition) ? 'Rejection--width2' : ''}>