[英]React JS: ForEach Function isn't returning anything
I have a JS function in my code that is supposed to return a React Component for Each element of my split string, but for some reason, when I call it on my main "return()", the function isn't returning anything.我的代码中有一个 JS function,它应该为拆分字符串的每个元素返回一个 React 组件,但由于某种原因,当我在主“return()”上调用它时,function 没有返回任何内容。 I tried to put the function directly inside of the return(), but the same happened.
我试图将 function 直接放在 return() 中,但同样的事情发生了。 That never happened to me before and I have no idea of what it is.
这从来没有发生在我身上,我不知道它是什么。
Here is my code:这是我的代码:
import React from 'react';
import DatabaseLiLine from './DatabaseLiLine';
const DatabaseLiSection = ({ className, children, classId }) => {
const example = "Use,This,As,An,Example";
const splitLiTitles = example.split(",");
const returnLines = () => {
splitLiTitles.forEach(element => {
return(
<DatabaseLiLine>
{element}
</DatabaseLiLine>
);
})
}
return (
<li className={className}>
<a href="/">{children}</a>
<div id={classId} className="consult_box">
{returnLines()}
</div>
</li>
);
}
export default DatabaseLiSection;
forEach doesn't return anything. forEach 不返回任何内容。 Change your code to use
map
method.更改您的代码以使用
map
方法。
const returnLines = () => {
splitLiTitles.map(element => {
return(
<DatabaseLiLine>
{element}
</DatabaseLiLine>
);
})
}
Having a little brain fart here but I forget if you also need to return the map method, like adding return
before splitLiTitles.map...
.这里有点脑子放屁,但我忘记了您是否还需要返回 map 方法,例如在 splitLiTitles.map 之前添加
return
splitLiTitles.map...
。 I think it should work without it, but if it doesn't try adding that return in front as well.我认为没有它它应该可以工作,但如果它不尝试在前面添加该返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.