UNPKG

3.99 kBMarkdownView Raw
1# Link and NavLink
2
3Auxiliary wrappers around [React Router](https://github.com/ReactTraining/react-router)'s
4`<Link>` and `<NavLink>` components; they help to handle external and internal
5links in a single uniform manner.
6
7[Examples](#examples)
8
9**Why?** &mdash; We use React Router to handle routing of our applications.
10React Router's `<Link>` and `<NavLink>` components work only with the
11application's internal links; to properly handle external links it is necessary
12to render them as `<a>` HTML elements. Our custom wrappers hide the difference
13between internal and extrenal links, allowing to handle them via the same
14interface provided by React Router, and extended with a few extra properties for
15advance behaviours. It is convenient both when the rendered links come from a
16visitor / database (thus you don't have to check yourself whether they are
17external or internal), and also when you have to frequently change the actual
18link addresses, as it often happens during active development / prototyping.
19
20Our `<Link>` and `<NavLink>` are rendered as simple `<a>` elements when:
211. The link is absolute, i.e. starts with `http://` or `https://`;
222. The link points to an anchor, i.e. starts with `#` symbol;
233. The link should be opened in a new tab (`openNewTab` property);
244. Explicitly opted by the `enforceA` property;
25
26Otherwise, `<Link>` and `<NavLink>` are rendered as the corresponding React
27Router's components. Additionally in this case, the links to the current page,
28when clicked, scroll the page to the top.
29
30Both `<Link>` and `<NavLink>` support all properties of the underlying React
31Router's components, along with some additional props:
32
33### Link properties
34- **`children`** &mdash; *Node* &mdash; Optional. Child ReactJS node to render
35inside the link;
36- **`className`** &mdash; *String* &mdash; Optional. Class(es) to apply to the
37rendered link;
38- **`enforceA`** &mdash; *Boolean* &mdash; Optional. If *true* enforces
39rendering of the link as a simple `<a>` element;
40- **`onClick`** &mdash; *Function* &mdash; Optional. An event handler to trigger
41on click;
42- **`onMouseDown`** &mdash; *Function* &mdash; Optional. An event handler to
43trigger on MouseDown event;
44- **`openNewTab`** &mdash; *Boolean* &mdash; Optional. If *true* the link will
45be opened in a new tab;
46- **`replace`** &mdash; *Boolean* &mdash; Optional. When *true*, clicking the
47link will replace the current entry in the history stack instead of adding a new
48one;
49- **`to`** &mdash; *String* &mdash; Optional. Link URL. Defaults to empty
50string.
51
52### NavLink properties
53`<NavLink>` supports all properties of `<Link>`, listed above, and the following
54additional ones, coming from React Router:
55- **`activeClassName`** &mdash; *String* &mdash; Optional. Class(es) to apply to
56the rendered link when it is active;
57- **`activeStyle`** &mdash; *String* &mdash; Optional. Styles to apply to the
58rendered link when it is active;
59- **`exact`** &mdash; *Boolean* &mdash; Optional. When *true*, the active
60class/style will only be applied if the location is matched exactly;
61- **`isActive`** &mdash; *Function* &mdash; Optional. A function to add extra
62logic for determining whether the link is active. This should be used if you
63want to do more than verify that the link’s pathname matches the current URL’s
64pathname;
65- **`location`** &mdash; *Object* &mdash; Optional. The `isActive` compares the
66current history location (usually the current browser URL). To compare to a
67different location, a `location` can be passed.
68- **`strict`** &mdash; *Boolean* &mdash; Optional. When `true`, the trailing
69slash on a location’s pathname will be taken into consideration when determining
70if the location matches the current URL. See the <Route strict> documentation
71for more information.
72
73### <a name="examples">Examples</a>
74
75Minimal `<Link>` example:
76```js
77import { Link } from 'topcoder-react-utils';
78
79export default function LinksExample() {
80 return <Link to="some/url">Link to Some URL</a>;
81}
82```