英文:
Is it okay to render basic JSX as an invoked function inside the render method?
问题
在我们的代码库中,我一直遇到这种在render
方法内部调用renderXelement()
的模式。我听说这对于React来说并不是一种很常见的模式。此外,我想知道是否有必要尽量避免这种模式,从某种意义上说它有哪些不利之处(如果有的话)。
请看 renderCollapsedLinks()
。我删除了一些多余的代码,以使问题更清晰。
示例:
export function NavigationDropdownLinksOverlay({
activeDropdownLink,
onClickBack,
}: NavigationDropdownLinkOverlay) {
const { classes } = useStyles();
const renderCollapsedLinks = (groupLink: DropdownGroupLinks) => {
return groupLink.links.map((link, index) => (
<Box mb={index !== groupLink.links.length - 1 ? 'sm' : 0}>
<Link href={link.link} className={classes.anchorLink}>
{link.label}
</Link>
</Box>
));
};
return (
<MobileNavigationOverlay show={!!activeDropdownLink}>
<MobileMenuBanner title={activeDropdownLink?.label || ''} />
<BackButton onClick={onClickBack} />
<Grid gutter={0} className={classes.linkGridFullWidth}>
{renderCollapsedLinks()}
</Grid>
</MobileNavigationOverlay>
);
}
请注意,我并没有回答是否应该避免这种模式的问题,而只是提供了代码的翻译部分。
英文:
In our codebase, I keep coming across this pattern of invoking renderXelement() inside the render method. I have heard that it is not a very common pattern for React. Also, I was wondering whether is a thing to avoid at all costs, in a sense that what kind of downsides it has (if any).
See renderCollapsedLinks(). I removed some of the excessive code to make the question more clear.
Example:
export function NavigationDropdownLinksOverlay({
activeDropdownLink,
onClickBack,
}: NavigationDropdownLinkOverlay) {
const { classes } = useStyles();
const renderCollapsedLinks = (groupLink: DropdownGroupLinks) => {
return groupLink.links.map((link, index) => (
<Box mb={index !== groupLink.links.length - 1 ? 'sm' : 0}>
<Link href={link.link} className={classes.anchorLink}>
{link.label}
</Link>
</Box>
));
};
return (
<MobileNavigationOverlay show={!!activeDropdownLink}>
<MobileMenuBanner title={activeDropdownLink?.label || ''} />
<BackButton onClick={onClickBack} />
<Grid gutter={0} className={classes.linkGridFullWidth}>
{renderCollapsedLinks()}
</Grid>
</MobileNavigationOverlay>
);
}
答案1
得分: 1
当然,你可以使用函数来计算一个值。即使在React中,即使这个值是JSX。
你甚至可以更进一步,将这个函数移到组件之外。这样做不仅让组件更易阅读,还有一个优势,即这个函数不再在每次渲染时都被创建:
export function NavigationDropdownLinksOverlay({
activeDropdownLink,
onClickBack,
}: NavigationDropdownLinkOverlay) {
const { classes } = useStyles();
return (
<MobileNavigationOverlay show={!!activeDropdownLink}>
<MobileMenuBanner title={activeDropdownLink?.label || ''} />
<BackButton onClick={onClickBack} />
<Grid gutter={0} className={classes.linkGridFullWidth}>
{renderCollapsedLinks(groupLink.links, classes)}
</Grid>
</MobileNavigationOverlay>
);
}
// renderCollapsedLinks现在是一个纯函数,仅依赖于函数参数来生成返回值。
const renderCollapsedLinks = (links, classes) => {
const last = links.length-1;
return links.map((link, index) => (
// 你应该为<Box />添加一个`key`
<Box mb={index !== last ? 'sm' : 0}>
<Link href={link.link} className={classes.anchorLink}>
{link.label}
</Link>
</Box>
));
};
英文:
Of course you can use functions to compute a value. Even in React, even if this value is JSX.
You can even go a step further and move this function out of the component. This makes imo. the component more readable and has the advantage that this function is no longer created on every render:
export function NavigationDropdownLinksOverlay({
activeDropdownLink,
onClickBack,
}: NavigationDropdownLinkOverlay) {
const { classes } = useStyles();
return (
<MobileNavigationOverlay show={!!activeDropdownLink}>
<MobileMenuBanner title={activeDropdownLink?.label || ''} />
<BackButton onClick={onClickBack} />
<Grid gutter={0} className={classes.linkGridFullWidth}>
{renderCollapsedLinks(groupLink.links, classes)}
</Grid>
</MobileNavigationOverlay>
);
}
// renderCollapsedLinks is now a pure function and relies only on the function arguments to produce the returned value.
const renderCollapsedLinks = (links, classes) => {
const last = links.length-1;
return links.map((link, index) => (
// you should add a `key` to `<Box />`
<Box mb={index !== last ? 'sm' : 0}>
<Link href={link.link} className={classes.anchorLink}>
{link.label}
</Link>
</Box>
));
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论