javascript - 在 React 中使用 js 创建 html 块
问题描述
我创建了一个函数,该函数通常为数组中的每个数据创建 html 块,但它不起作用。有人能帮助我吗 。
这是功能:
makeGrid (){
var data = test.map( x => [ x.name, x.price, ] );
data.forEach(function(element) {
var elem = () => ({
type: 'div',
props: {
className: 'box',
children: [{
type: 'i',
props: {
className: 'hvhv'
}
}, {
type: 'h3',
props: {
children: element[0]
}
}, {
type: 'p',
props: {
children: element[1]
}
}]
}
});
return elem;
});
}
在这里我执行了这个功能:
render() {
return (
<div className = "Grid" >
< section className = "boxes" >
<h1> {this.makeGrid()} </h1>
< /section> < /div > );
}
}
export default Grid;
我希望这些块像:
< div className = "box" >
< i className = "fas fa-chart-pie fa-4x" > < /i>
<h3 > Analytics < /h3 >
<p > Lorem ipsum dolor sit amet, consectetur adipisicing elit.Quasi, expedita ? < /p > < /div >
解决方案
你可以添加return
到forEach
makeGrid (){
var data = test.map( x => [ x.name, x.price, ] );
return data.forEach(function(element) {
var elem = () => ({
type: 'div',
props: {
className: 'box',
children: [{
type: 'i',
props: {
className: 'hvhv'
}
}, {
type: 'h3',
props: {
children: element[0]
}
}, {
type: 'p',
props: {
children: element[1]
}
}]
}
});
return elem;
});
}
推荐阅读
- python - 堆叠/取消堆叠时如何不使用 NaN 松散行?
- javascript - 将动态参数从 URL 传递到我页面中的外部链接
- flutter - 如何使用飞镖颤振作为启动画面绘制动画徽标
- r - 从提取邮件中反转名称字符串
- c++ - While 循环不会无限循环(C++)
- python - 在 Python 中编织数字
- facebook - 尝试实施 Facebook Conceal 方法来保存加密密码但没有用?
- android - Flutter video_player 无法在一页加载多个视频
- swift - 从视图返回时,Rx BehaviorRelay 不保留数据
- python - 我在 cmd 上运行 pytest,我得到了 AssertionError