reactjs - 从 switch 语句中渲染两个组件 react
问题描述
目标:在 switch 语句中返回两个组件。具体来说,如何渲染我的 LinkButtonTypes
&LinkList
组件?
这是我的开关语句:
const switchComponent = ({ selectedSidebar }) => {
let component;
switch (selectedSidebar) {
// main dashboard components
case "dashboard":
component = <LinkButtonTypes setLinkType={setLinkType} /> && (
<LinkList/>
);
break;
// render editor components
case "skins":
component = <SkinsEditor />;
break;
default:
console.log("Unknwon switch component");
}
return component;
};
这是我渲染组件的地方:
<div className="dash__middle">
{switchComponent({ selectedSidebar })}
</div>
目前,switch 语句只呈现我的LinkButtonTypes
组件。
如何渲染我的 LinkButtonTypes
&LinkList
组件?
解决方案
您可以使用Fragment
,如下所示:
const switchComponent = ({ selectedSidebar }) => {
let component;
switch (selectedSidebar) {
// main dashboard components
case "dashboard":
component = (
<React.Fragment>
<LinkButtonTypes setLinkType={setLinkType} />
<LinkList/>
</React.Fragment>
);
break;
// render editor components
case "skins":
component = <SkinsEditor />;
break;
default:
console.log("Unknwon switch component");
}
return component;
};
推荐阅读
- python - Python Selenium:无法找出可点击链接元素的定位器
- java - 动态加载的弹簧上下文
- pdf - 有没有办法区分原生和扫描的pdf?
- python - 如何创建这样的数组
- python - 如何将 len() 的输出作为函数参数的默认值传递
- excel - 从活动单元格选择到上面的特定单元格
- c++ - 为什么 const char[] 和 const char* 之间的比较不同?
- java - 如何从类中的每个方法中获取 MethodCallExpr name 和 FieldAccessExpr 的名称?
- python - 如何在numpy数组中生成给定模式的每个组合?
- google-cloud-platform - 如何授予对托管在 Google Compute Engine 的 VM 上的 Java Web 应用程序的访问权限