以下是一个遍历数组并渲染JSX元素的示例代码:
import React from 'react';
const MyComponent = () => {
const data = ['apple', 'banana', 'orange'];
return (
{data.map((item, index) => (
{item}
))}
);
};
export default MyComponent;
在上述示例中,我们定义了一个数组 最后,我们将所有渲染的
上一篇:遍历数组并写入错误消息
下一篇:遍历数组并选择每两个元素 data,它包含了一些水果的名称。然后,我们使用map函数遍历数组,并为每个数组元素渲染一个包含元素名称的key属性,我们使用了index作为key值。
MyComponent时,就会得到一个包含了所有水果名称的元素列表。
相关内容