简体   繁体   English

在 react js 的组件内渲染一个函数

[英]Render a function inside a component in react js

My target is to create a breadcrumb component in react js.我的目标是在 react js 中创建一个面包屑组件。 I used Ant Design in my App, but I have some issues with a parth of it.我在我的应用程序中使用了 Ant Design,但我对它的一部分有一些问题。

In the documentation of Ant Design i found a solution for creating dynamic breadcrumbs, but can't find out how to apply the code.在 Ant Design 的文档中,我找到了创建动态面包屑的解决方案,但找不到如何应用代码。

Using, Ant design i built the next app:使用 Ant 设计,我构建了下一个应用程序:

 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;

Also, the return statement is located outside of the function and i get error due of this.此外,返回语句位于函数之外,因此我收到错误。

How to create, using this implementation, a breadcrumb, and how to render itemRender function inside my component, and from where should i get these params itemRender(route, params, routes, paths) ?如何使用此实现创建面包屑,以及如何在我的组件内呈现itemRender函数,以及我应该从哪里获取这些参数itemRender(route, params, routes, paths)

You need to use Breadcrum component in App body您需要在 App body 中使用Breadcrum组件

function App() {
  return (
      <div>
        <Breadcrumb itemRender={itemRender} routes={routes} />
      </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM