React - 使用带有前缀的道具作为元素 ID 的正确语法?
React - correct syntax to use a prop with a prefix as an ID for an element?
对元素 id 使用 prop 的正确语法是什么,但带有前缀。我是否必须将每个分配给一个变量并使用字符串文字,或者我可以内联吗?
例如
<input id="first_{id}" type="text" value={firstName} />
<input id="last_{id}" type="text" value={lastName} />
在您的值之外使用 ${}
,在 javascript
中称为 template literals
{`your-prefix-${yourValue}`}
我更喜欢像这样的字符串插值语法
<input id={`first_${id}`} type="text" value={firstName} />
对元素 id 使用 prop 的正确语法是什么,但带有前缀。我是否必须将每个分配给一个变量并使用字符串文字,或者我可以内联吗?
例如
<input id="first_{id}" type="text" value={firstName} />
<input id="last_{id}" type="text" value={lastName} />
在您的值之外使用 ${}
,在 javascript
{`your-prefix-${yourValue}`}
我更喜欢像这样的字符串插值语法
<input id={`first_${id}`} type="text" value={firstName} />