javascript - why styles don't apply on dangerouslySetInnerHTML - Stack Overflow

i need to render some styled jsx tags in my ponent by using useTranslation hook and dangerouslySetInner

i need to render some styled jsx tags in my ponent by using useTranslation hook and dangerouslySetInnerHTML, those tags are stored in a json file , i found that dangerouslySetInnerHTML work's fine but don't apply tailwind styles for each tag,

json file:

{
   "about-text":"<div className='text-blue px-5'><p>Text ...</p</div>",
}

inside ponenets:

<div className="p-10">
<div
   dangerouslySetInnerHTML={{
      __html: t('about-text'),
    }}
   />
</div>

i need to render some styled jsx tags in my ponent by using useTranslation hook and dangerouslySetInnerHTML, those tags are stored in a json file , i found that dangerouslySetInnerHTML work's fine but don't apply tailwind styles for each tag,

json file:

{
   "about-text":"<div className='text-blue px-5'><p>Text ...</p</div>",
}

inside ponenets:

<div className="p-10">
<div
   dangerouslySetInnerHTML={{
      __html: t('about-text'),
    }}
   />
</div>
Share Improve this question edited Jan 24, 2022 at 16:58 Escanor asked Jan 24, 2022 at 16:54 EscanorEscanor 953 silver badges10 bronze badges 2
  • 2 Will the JSON file you've shown here be matched by the paths that you have provided to content in Tailwind's configuration? If not, Tailwind won't pile those classes (assuming they're not used anywhere else). – George Commented Jan 24, 2022 at 16:57
  • Could it be because about-text contains broken html? – spender Commented Jan 24, 2022 at 16:57
Add a ment  | 

2 Answers 2

Reset to default 8

dangerouslySetInnerHTML expects to be passed a string of HTML, not a string of JSX.

The class attribute is used directly. You can't adjust the className property instead.

This is an example of a tag that worked for me, using Tailwind:

<a class="hover:text-blue-600" href="some youtube link" target="_blank">https://.</a>

Just use "class" instead of "className" for the dangerouslySetInnerHTML CSS class.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744177674a4561832.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信