1. classNames
1.1 ์ค์น
1yarn add classnames
1.2 ๊ฐ๋จํ ์ฌ์ฉ๋ฒ
1import classNames from 'classnames'23classNames('one', 'two') // = โone twoโ4classNames('one', { two: true }) // = โone twoโ5classNames('one', { two: false }) // = โoneโ6classNames('one', ['two', 'three']) // = โone two threeโ78const myClass = 'hello'9classNames('one', myClass, { myCondition: true }) // = โone hello myCondition'
์ด๋ฐ ์์ผ๋ก ์ฌ๋ฌ ๊ฐ์ง ์ข ๋ฅ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์กฐํฉํด CSS ํด๋์ค๋ฅผ ์ค์ ํ ์ ์๊ธฐ ๋๋ฌธ์, ์ปดํฌ๋ํธ์์ ์กฐ๊ฑด๋ถ๋ก ํด๋์ค๋ฅผ ์ค์ ํ ๋ ๋งค์ฐ ์ ์ฉํ๋ค.
1/**2* highlighted ๊ฐ์ด true๋ฉด, highlighted ํด๋์ค๊ฐ ์ ์ฉ3* theme์ผ๋ก ์ ๋ฌ๋ฐ๋ ๋ฌธ์์ด์ ๋ด์ฉ ๊ทธ๋๋ก ํด๋์ค์ ์ ์ฉ4*/5const MyComponent = ({ highlighted, theme }) => (6<div className={classNames('MyComponent', { highlighted }, theme)}>Hello</div>7)
props ๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ์คํ์ผ์ ์ฃผ๊ธฐ๊ฐ ์ฝ๋ค.
๋ฐ๋๋ก ์ ์ฝ๋๋ฅผ classnames๋ฅผ ์ฐ์ง ์๊ณ ์ ์ฉํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
1const MyComponent = ({ highlighted, theme }) => (2<div className={`MyComponent ${theme} ${highlighted ? 'highlighted' : ''}`}>Hello</div>3)