我希望能够动态设置React元素上的“key”属性,如下所示。然而,这两种方法都不起作用。我知道我可以使用其他技术来实现这一点,但我想知道是否有可能以这种方式实现:
错误提示
第一种方法:
{
new Array(3).fill(<td></td>).map((td,i) => {
td.setAttribute('key', i);
return td;
})
}
二:
{
new Array(3).fill(document.createElement(td)).map((td,i) => {
td.setAttribute('key', i);
return td;
})
}
错误提示
// td is {$$typeof: Symbol(react.element), type: "td", key: null, ref: null, props: {…}, …}
// Uncaught (in promise) TypeError: td.setAttribute is not a function