Skip to content

Instantly share code, notes, and snippets.

@xelaz
Created January 22, 2020 15:12
Show Gist options
  • Save xelaz/9fe5a371e64787101b28c47991e07661 to your computer and use it in GitHub Desktop.
Save xelaz/9fe5a371e64787101b28c47991e07661 to your computer and use it in GitHub Desktop.
Simple class names concatenation in ES6 for React and others
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