Javascript 如何动态地在React JSX元素上设置属性?

Javascript 如何动态地在React JSX元素上设置属性?,javascript,reactjs,iteration,jsx,setattribute,Javascript,Reactjs,Iteration,Jsx,Setattribute,我想动态地在React元素上设置key属性,如下所示。然而,这两种方法都不起作用。我知道我可以使用其他技术,但我想知道是否有可能这样做: 一: 错误 // td is {$$typeof: Symbol(react.element), type: "td", key: null, ref: null, props: {…}, …} // Uncaught (in promise) TypeError: td.setAttribute is not a function 这两种方法将相同的td元

我想动态地在React元素上设置
key
属性,如下所示。然而,这两种方法都不起作用。我知道我可以使用其他技术,但我想知道是否有可能这样做:

一:

错误

// td is {$$typeof: Symbol(react.element), type: "td", key: null, ref: null, props: {…}, …}
// Uncaught (in promise) TypeError: td.setAttribute is not a function

这两种方法将相同的
td
元素放在数组中三次。绝对不是你想做的。您要创建三个
td
s。您可以使用
null
填充,然后映射到元素中:

{
    Array(3).fill(null).map((_, i) => <td key={i}></td>)
}
实例:

const行=()=>
{
from(数组(3),(_,i)=>TD{i})
}
;
ReactDOM.render(
,
document.getElementById(“根”)
);

// td is {$$typeof: Symbol(react.element), type: "td", key: null, ref: null, props: {…}, …}
// Uncaught (in promise) TypeError: td.setAttribute is not a function
{
    Array(3).fill(null).map((_, i) => <td key={i}></td>)
}
{
  Array.from(Array(3), (_, i) => <td key={i}></td>)
}