javascript - 在 React js 中渲染组件内部的函数
问题描述
我的目标是在 react js 中创建一个面包屑组件。我在我的应用程序中使用了 Ant Design,但其中一部分存在一些问题。
在 Ant Design 的文档中,我找到了创建动态面包屑的解决方案,但找不到如何应用代码。
使用 Ant design 我构建了下一个应用程序:
import React from "react";
import {Link, BrowserRouter, Route} from "react-router-dom";
import {Breadcrumb} from 'antd';
//from here starts the code from Ant Design Documentation
const routes = [
{
path: 'index',
breadcrumbName: 'home',
},
{
path: 'first',
breadcrumbName: 'first',
children: [
{
path: '/general',
breadcrumbName: 'General',
},
{
path: '/layout',
breadcrumbName: 'Layout',
},
{
path: '/navigation',
breadcrumbName: 'Navigation',
},
],
},
{
path: 'second',
breadcrumbName: 'second',
},
];
function itemRender(route, params, routes, paths) {
const last = routes.indexOf(route) === routes.length - 1;
return last ? (
<span>{route.breadcrumbName}</span>
) : (
<Link to={paths.join('/')}>{route.breadcrumbName}</Link>
);
}
return <Breadcrumb itemRender={itemRender} routes={routes} />;
//here is the end of the code from Ant Design
function App() {
return (
<div>
<p>here i want to render my breadcrumb</p>
</div>
);
}
export default App;
此外,return 语句位于函数之外,因此我得到错误。
如何使用此实现创建面包屑,以及如何itemRender
在我的组件中呈现函数,以及我应该从哪里获得这些参数itemRender(route, params, routes, paths)
?
解决方案
您需要Breadcrum
在 App body 中使用组件
function App() {
return (
<div>
<Breadcrumb itemRender={itemRender} routes={routes} />
</div>
);
}
推荐阅读
- python - 缺少 1 个必需的位置参数:“密钥”加密
- javascript - 未捕获(承诺中)类型错误:无法读取 null 的属性“0”。如果超过 4 个标记,谷歌地图地理编码器
- javascript - 简单的香草 JavaScript 事件侦听器不起作用
- r - 将每日日期转换为 R 中的两周
- matplotlib - 如何将 3 个 seaborn 散点图放在一起?
- android - 如何在 AppBar 下方创建一个裁剪的 NavigationDrawer?
- c# - CrystalReportViewer 给出错误,“创建组件失败”
- php - 为什么我在 Laravel 中为 foreach 提供无效的参数以进行 json 响应?
- php - 在 Codeigniter 中过滤 Soap 请求
- php - 如何使用 php 和 Mysqli 在 Meta 关键字中使用多个数据