Created
January 22, 2020 15:12
-
-
Save xelaz/9fe5a371e64787101b28c47991e07661 to your computer and use it in GitHub Desktop.
Simple class names concatenation in ES6 for React and others
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
const classNames = (...args) => args.reduce((list, clss) => list.concat(clss).filter(Boolean), []) | |
.join(' ').trim().replace(/\s{2,}/g, ' '); | |
const test = null; | |
const test2 = true; | |
console.log(classNames(test && 'test1')); | |
console.log(classNames(test2 && 'test2')); | |
console.log(classNames(['test1', 'test2', test && 'test3', test2 && 'test4'], 'test5')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment