DynamicLink.tsx 588 B

123456789101112131415161718192021222324
  1. import React from "react";
  2. import { Link, LinkProps } from "react-router-dom";
  3. const DynamicLink: React.FC<LinkProps> = ({ to, children, ...props }) => {
  4. // It is a simple element with nothing to link to
  5. if (!to) return <span {...props}>{children}</span>;
  6. // It is intended to be an external link
  7. if (typeof to === "string" && /^https?:\/\//.test(to))
  8. return (
  9. <a href={to} {...props}>
  10. {children}
  11. </a>
  12. );
  13. // Finally, it is an internal link
  14. return (
  15. <Link to={to} {...props}>
  16. {children}
  17. </Link>
  18. );
  19. };
  20. export default DynamicLink;