i've been musing on how best conditionally apply css class in react js. i've seen answers banding around there aren't many out there or they're not elaborative i'd like.
the react documentation on manipulating class names suggests classnames
npm package. docs package great. following snippet straight package's readme:
classnames('foo', 'bar'); // => 'foo bar' classnames('foo', { bar: true }); // => 'foo bar' classnames({ 'foo-bar': true }); // => 'foo-bar' classnames({ 'foo-bar': false }); // => '' classnames({ foo: true }, { bar: true }); // => 'foo bar' classnames({ foo: true, bar: true }); // => 'foo bar' // lots of arguments of various types classnames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux' // other falsy values ignored classnames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
Comments
Post a Comment