reactjs - 将函数作为道具传递
问题描述
尝试将 {} 放在作为道具传递的函数周围时,我遇到了“渲染中没有返回任何内容”问题。我想也许我把它们放在了错误的地方。
我应该将 {} 放在此代码的什么位置?
const WhiteAdvantageTags = props => {
props.whiteAdvantageTags.map(function (name1, index1) {
return (
<Typography
style={{
lineHeight: '2',
marginLeft: '0px',
padding: '0',
}}
>
<Link
href="#"
onClick={event => {
props.popupInstruction(name1, 'White');
}}
onMouseOver={event => {
props.highlightSquares(name1, 'White');
}}
onMouseLeave={event =>
props.board.current.removeMarkers(undefined, undefined)
}
variant="body2"
>
- {name1}
</Link>
</Typography>
);
});
};
解决方案
为了使用 {} 大括号来包装您的函数,您需要确保从该函数显式返回某些内容。在这个例子中,用大括号实现它的正确方法是:
const WhiteAdvantageTags = props => {
return props.whiteAdvantageTags.map(function (name1, index1) { // added return statement
return (
<Typography
style={{
lineHeight: '2',
marginLeft: '0px',
padding: '0',
}}
>
<Link
href="#"
onClick={event => {
props.popupInstruction(name1, 'White');
}}
onMouseOver={event => {
props.highlightSquares(name1, 'White');
}}
onMouseLeave={event =>
props.board.current.removeMarkers(undefined, undefined)
}
variant="body2"
>
- {name1}
</Link>
</Typography>
);
});
};
但是,由于该函数只是返回此值,因此您可以直接返回它:
const WhiteAdvantageTags = props => props.whiteAdvantageTags.map(function (name1, index1) {
return (
<Typography
style={{
lineHeight: '2',
marginLeft: '0px',
padding: '0',
}}
>
<Link
href="#"
onClick={event => {
props.popupInstruction(name1, 'White');
}}
onMouseOver={event => {
props.highlightSquares(name1, 'White');
}}
onMouseLeave={event =>
props.board.current.removeMarkers(undefined, undefined)
}
variant="body2"
>
- {name1}
</Link>
</Typography>
);
});
推荐阅读
- windows - 使用 Powershell 以方便的格式列出系统信息
- ssl - FIPS 模式与 NON-FIPS 模式下密码套件列表的差异
- ubuntu - 尝试通过substrate-front-end-template访问Substrate节点错误{“isTrusted”:true}
- google-sheets - Google Sheet 将 ArrayFormula 与 textjoin 结合使用
- html - HTML 未与 Hugo 中的 CSS 链接
- jdbc - 为复合行实现 SQLData 接口
- javascript - 有没有办法在每一步更改时调用一个函数?
- android - 为什么我可以在低于`build.gradle`中指定的`minSdkVersion`的设备上安装与我的设备兼容的应用程序版本?
- ansible - 如何在角色中访问 ansible 事实?
- java - 如何在android中实现字典