preface

There is no technical difficulty, just a little more judgment and modification than the official documents;

With react-Router-breadcrumbs -hoc, both contracted and configured routing is available,

Just pass in the data format that meets the specification

Based on the environment

React 16.4/UmiJS 2.2/Antd 3.x/ React -router-breadcrumbs-hoc 2.x: THE API is concise and easy to understand

rendering

  • nonantdstyle

  • antdstyle

Source code and implementation

Exposure of props:

  • data: breadcrumbs mapping, array (react-router-breadcrumbs-hocThe required kind)

Const routes = [{path: ‘/’, breadcrumb: ‘home’}];

If not, the high-order component (react-router-breadcrumbs-hoc) automatically gets the path name as the crumbs name

Breakcrumbs

import Link from 'umi/link';
import withBreadcrumbs from 'react-router-breadcrumbs-hoc';
import { Breadcrumb, Badge, Icon } from 'antd';

/ / more configuration please click https://github.com/icd2k3/react-router-breadcrumbs-hoc
const routes = [{ path: '/'.breadcrumb: 'home' }];

const Breadcrumbs = ({ data }) = > {
  if (data && Array.isArray(data)) {
    const AntdBreadcrumb = withBreadcrumbs(data)(({ breadcrumbs }) = > {
      return (
        <Breadcrumb separator={<Icon type="double-right" />} classNames="spread">
          {breadcrumbs.map((breadcrumb, index) => (
            <Breadcrumb.Item key={breadcrumb.key}>
              {breadcrumbs.length - 1 === index ? (
                <Badge status="processing" text={breadcrumb} />
              ) : (
                <Link
                  to={{
                    pathname: breadcrumb.props.match.url,
                    state: breadcrumb.props.match.params ? breadcrumb.props.match.params : {},
                    query: breadcrumb.props.location.query ? breadcrumb.props.location.query : {},
                  }}
                >
                  {breadcrumb}
                </Link>
              )}
            </Breadcrumb.Item>
          ))}
        </Breadcrumb>
      );
    });
    return <AntdBreadcrumb />;
  }
  const DefaultBreadcrumb = withBreadcrumbs(routes)(({ breadcrumbs }) => (
    <div>
      {breadcrumbs.map((breadcrumb, index) => (
        <span key={breadcrumb.key}>
          <Link
            to={{
              pathname: breadcrumb.props.match.url,
              state: breadcrumb.props.match.params ? breadcrumb.props.match.params : {},
              query: breadcrumb.props.location.query ? breadcrumb.props.location.query : {},
            }}
          >
            {breadcrumb}
          </Link>
          {index < breadcrumbs.length - 1 && <i> / </i>}
        </span>
      ))}
    </div>
  ));
  return <DefaultBreadcrumb />;
};

export default Breadcrumbs;


Copy the code

conclusion

If you return a higher-order component directly, an exception will be thrown saying that you returned a function instead of a react.child,

The solution is to cache a component as my code does and return a component directly

If there is something wrong, please leave a message and correct it in time. Thank you for reading.