reactjs - setting variables in a map function
问题描述
I have a function that returns this jsx mixed with some code like this:
showMedia = () => {
return (
<div>
{
<div id="mainContent">
{props.files.map((file) =>
const ext = fileExtension(file.name),
const icon = fileIcon(ext),
const isImg = isImage(ext),
<div key={file.id}>
<DisplayResults
fileId={file.id}
downloadName={file.downloadName}
fileTitle={file.title}
isImg={isImg}
icon={icon}
ext={ext}
/>
</div>
)}
</div>
}
</div>
);
}
In my editor, it's saying that it is expecting an '{' for the const variables I am setting after .map
Am I not using the correct syntax?
Thanks!
解决方案
Since you don't have a function body {}
for your arrow function, it is expecting an implicitly returned expression.
Give it a function body and declare your variables and return the JSX instead:
showMedia = () => {
return (
<div>
{
<div id="mainContent">
{props.files.map(file => {
const ext = fileExtension(file.name);
const icon = fileIcon(ext);
const isImg = isImage(ext);
return (
<div key={file.id}>
<DisplayResults
fileId={file.id}
downloadName={file.downloadName}
fileTitle={file.title}
isImg={isImg}
icon={icon}
ext={ext}
/>
</div>
);
})}
</div>
}
</div>
);
};
推荐阅读
- python - 我可以在英国开放天气地图中按邮政编码搜索吗
- jquery - 自定义事件在 Jquery 中触发,但找不到任何事件处理程序
- java - 不使用 JLayeredPane 将 jcomponent 移到后面
- python - from_point 函数不会在 windows CMD 上返回预期的控件
- eclipse - 为 Eclipse 安装 groovy 插件时出现问题
- spring-boot - SpringBoot 控制器如何知道返回胡子模板而不是字符串
- visual-studio - 我无法让 log4net 过滤工作 - 似乎忽略了级别和过滤器设置
- android - 'it' 会在内部自动进行智能转换吗?
- python - 如何使用 python win32 循环遍历 excel 数据透视切片器?
- c# - 关于保存相关实体的 EF 与 EF Core