admin管理员组文章数量:1310875
Given the following:
function generateWrapper(...elements) {
return (
<div>
{...elements}
</div>
);
}
ReactDOM.render(
generateWrapper(
<MyElement name="first" />,
<MyElement name="second" />,
),
document.getElementById("app"),
);
React will rightfully plain that I haven't added a key property to each of the children of the wrapper div. How can I do this? Or is this against the spirit of react development? Thanks for any answers in advance!
PS: Yes there is a similar question I looked at but it seemed to be targeting users not using JSX
PPS: I do realize I can merely add a key to first
and second
MyElement
's but this is what I'm attempting to avoid
edit: Changing code to better suit my circumstances
Given the following:
function generateWrapper(...elements) {
return (
<div>
{...elements}
</div>
);
}
ReactDOM.render(
generateWrapper(
<MyElement name="first" />,
<MyElement name="second" />,
),
document.getElementById("app"),
);
React will rightfully plain that I haven't added a key property to each of the children of the wrapper div. How can I do this? Or is this against the spirit of react development? Thanks for any answers in advance!
PS: Yes there is a similar question I looked at but it seemed to be targeting users not using JSX
PPS: I do realize I can merely add a key to first
and second
MyElement
's but this is what I'm attempting to avoid
edit: Changing code to better suit my circumstances
Share Improve this question edited Jul 17, 2017 at 23:47 Right2Drive asked Jul 17, 2017 at 23:35 Right2DriveRight2Drive 1351 silver badge7 bronze badges 4- If you render a list, every ponents needs a key. – T4rk1n Commented Jul 17, 2017 at 23:50
- I pletely understand why there needs to be a key, and how I could go about doing it during instantiation. I'm just asking if it's possible to do it after instantiation so it could be done once in generateWrapper instead of for every single ponent – Right2Drive Commented Jul 17, 2017 at 23:56
-
I normally use
[].map((e, i) => <div key={i}>{e}</div>)
. – T4rk1n Commented Jul 18, 2017 at 0:00 -
I have also been remended elsewhere to create a wrapper ponent for
MyElement
that adds a key toMyElement
, just to provide all the alternatives I have received for those looking for answers as well – Right2Drive Commented Jul 18, 2017 at 0:00
1 Answer
Reset to default 11Take a look at React.cloneElement. You could use that to create and attach the key property inside your generateWrapper
method.
function generateWrapper(...elements) {
return (
<div>
{
elements.map(element =>
React.cloneElement(element, { key: 'your-unique-key-here' })
}
</div>
);
}
本文标签: javascriptCan you add a key to a React JSX Element after it39s instantiatedStack Overflow
版权声明:本文标题:javascript - Can you add a key to a React JSX Element after it's instantiated? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741865014a2401848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论