React-使用前缀作为元素ID的prop的正确语法?

问题描述 投票:0回答:2

将prop用作元素id的正确语法是什么,除了前缀之外。我必须将每个变量分配给一个变量并使用字符串文字,还是可以内联吗?

例如

<input id="first_{id}" type="text" value={firstName} />
<input id="last_{id}" type="text" value={lastName} />
reactjs syntax react-props
2个回答
1
投票

在您的值之外使用${},在javascript中称为template literals

{`your-prefix-${yourValue}`}

0
投票

我更喜欢这样的字符串插值语法

<input id={`first_${id}`} type="text" value={firstName} />
© www.soinside.com 2019 - 2024. All rights reserved.