【发布时间】:2023-01-30 22:05:51
【问题描述】:
我想将一串文本添加到 JSX 元素的属性中。请看下面的示例,其中变量 otherAttributes 是一个字符串。实际上,在我的例子中,它是一个来自 CMS 的变量,我想将它添加为属性。
const otherAttributes = 'target="_blank" rel="follow"';
<a href={href || ''} class="inline-block px-4 py-2 rounded-full" {...otherAttributes}>{text}</a>
我得到输出为
<a 0=""" 1="t" 2="a" 3="r" 4="g" 5="e" 6="t" 7="=" 8="\" 9=""" 10="_" 11="b" 12="l" 13="a" 14="n" 15="k" 16="\" 17=""" 18=""" href="" class="inline-block px-4 py-2 rounded-full" >See more</a>
【问题讨论】:
-
在传播之前将
otherAttributes转换为对象 -
这就是为什么我问如何制作它。