react-omni-link
A universal link component for react and react-router, this library encapsulates <a>
element from DOM and <NavLink>
component from react-router
, also it allows you to create any link component from url template.
Link
The basic component is the Link
named export, it receives props:
{string} to
: The target URL of link.{boolean} external
: Settarget="_blank"
to<a>
element, this also addsrel="noopener noreferrer"
prop.{boolean} refresh
: Use simple<a>
element instead of react-router's<Link>
component, which will lead a refresh of page when clicked.{string} activeClassName
: The same as react-router's<NavLink>
component, when this prop is provided a non empty value,<NavLink>
is used instead of<Link>
.
Other props will be passed down to <a>
element or <Link>
component.
Shortcut
This package also exports two components to fix the external
or refresh
prop:
const ExternalLink = <Link external ...props />; const HyperLink = <Link refresh ...props />;
Template Link
The createLink
named export is a function to create a unversal <Link>
component from a url template:
Component
The urlTemplate
parameter can be either a string or a template object generated by uri-templates package, the basic usage can be:
; // Without any propconst UserListLink = ; // Inline prop to urlconst UserProfileLink = ; // Inline prop without url encodingconst DocumentViewLink = ; // Expand object prop to query stringconst DocumentEditLink = ; // Default some propsconst AdminConsoleLink = ;
See uri-templates pacakge for details template syntax.