javascript - 从 JSX 内部的渲染访问数组
问题描述
我正在尝试从返回内部的渲染中遍历返回的数组。
class Example extends React.Component {
render() {
let actortype = 1;
const arr1 = ['a', 'b', 'c'];
const arr2 = ['x', 'y', 'z'];
if (actortype == 1) {
var array = [...arr1]; //clone arr1 to array
} else {
var array = [...arr2];
}
return (
<div>
//I want to map through the array returned above in render. I am doing something like this:
array.map((index)=>{
<div>testing</div>
})
// seems not the right approach to access array inside return. returns plain text
</div>
);
}
}
请分享您的反馈,我该怎么做。
解决方案
在 , 中使用 JSX 表达式 ( {}
)div
来调用map
:
<div>
{array.map( item => <div>{item}</div>})}
</div>
并使用return
或使用简洁的箭头函数(我在上面完成了后者)。有关后一点的更多信息,请参阅为什么我的箭头函数不返回任何内容?
推荐阅读
- halide - 从 halide::buffer 构造一个 halide::runtime:buffer
- jquery - 根据表单输入在css中动画进度条(可能的Jquery)
- python - XGBRegressor 不断返回 100% 的准确率
- android - Android X 中的 Google Map and Place 问题
- c++ - 什么是 std::false_type 或 std::true_type?
- linux - 为什么bash文本输出和下一个提示之间没有空格以及如何修复它?
- javascript - 扩展运算符在数组中添加带有键的元素
- excel - 使用 .find 根据 csv 文件中的列名返回列号
- php - 我如何动态更改下拉列表中复选框基础的值
- objective-c - 如何在推送的 ViewController (Xcode) 中获得后退按钮