"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > Tailwind CSS에서 동적 클래스 수정을 위해 템플릿 리터럴을 올바르게 사용하는 방법은 무엇입니까?

Tailwind CSS에서 동적 클래스 수정을 위해 템플릿 리터럴을 올바르게 사용하는 방법은 무엇입니까?

2025-03-24에 게시되었습니다
검색:457

How to Correctly Use Template Literals for Dynamic Class Modification in Tailwind CSS?

문제가 발생할 수 있습니다. 템플릿 리터럴을 사용하여이를 달성하는 올바른 방법은

classname = {`절대 삽입 -0 $ {클릭? 'translate-x-0': '-translate-x-full'} transform z-400 h- 스크린 W-1/4 bg-blue-300`}

className=" absolute inset-0 ${click ? translate-x-0 : -translate-x-full } 
        transform  z-400 h-screen w-1/4 bg-blue-300 "
대안 적으로, 당신은 string concatenation을 사용하여 classname을 정의 할 수 있습니다 :

className={`absolute inset-0 ${click ? 'translate-x-0' : '-translate-x-full'} transform z-400 h-screen w-1/4 bg-blue-300`}

개별 클래스 이름에 대한 문자열 연결을 피하는 것이 중요합니다. 'red': 'green'} -600`}
className=" absolute inset-0 ${click ? translate-x-0 : -translate-x-full } 
        transform  z-400 h-screen w-1/4 bg-blue-300 "
대신 다음과 같이 클래스 이름 전체를 선택하십시오 :

classname = {`$ {error? 'Text-Red-600': 'Text-Green-600'}}}
className=" absolute inset-0 ${click ? translate-x-0 : -translate-x-full } 
        transform  z-400 h-screen w-1/4 bg-blue-300 "
className = {error? 'Text-Red-600': 'Text-Green-600'}

className={`${error ? 'text-red-600' : 'text-green-600'}`}
className=" absolute inset-0 ${click ? translate-x-0 : -translate-x-full } 
        transform  z-400 h-screen w-1/4 bg-blue-300 "

와 같은 라이브러리를 사용하여 Twin.macro, Twind 또는 Xwind와 같은 Tailwind-Specific 솔루션
  • 리소스 :
  • [반응식 클래스 이름을 조건부로 적용하는 반응 이름?] (https://stackoverflow.com/questions/24217807/how-to-dynamically-add-aclass-to-manual class-names)
  • [조건부 스타일링을 처리하는 올바른 방법 React] (https://stackoverflow.com/questions/40787489/correct-way-ton-condlitional-styling-in-react)
  • [Embedding Expressions jsx] (https://reactjs.org/docs/jsx-in-depth.html#embedding-expressions-in-jsx)

[템플릿 리터럴- MDN] (https://developer.mozilla.org/en-us/docs/web/javaScript/reference/template_literals) How to Correctly Use Template Literals for Dynamic Class Modification in Tailwind CSS?

[생산 최적화 - Purgeable HTML- Tailwind CSS] (https://tailwindcss.com/docs/optimizing-for-production#writing-purgable-html)

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3