1 | # NavLink
|
2 | `NavLink` is the a React component for navigational links. When the link is clicked, NavLink will dispatch `NAVIGATE` action to flux dispatcher. The dispatcher can then dispatch the action to the stores that can handle it.
|
3 |
|
4 | ## Example Usage
|
5 |
|
6 | Here are two examples of generating `NavLink` using `href` property, and using `routeName` property. Using `href` property is better than using `routeName`, because:
|
7 |
|
8 | * Using `href` makes your code more readible, as it shows exactly how the `href` is generated.
|
9 | * Using `routeName` assumes `this.context` or `this.props.context` has a `makePath()` function, which will be used to generate the `href` from the `routeName` and `navParams` props.
|
10 | * Using `routeName` could be more limited, especially when it comes to query string and hash fragment, if the `makePath()` function does not support query string and hash fragment.
|
11 |
|
12 | ### Example of Using `href` Property (Recommended)
|
13 |
|
14 | If the url is static, or you can generate the url outside of `Navlink`, you can simply pass the url to `NavLink` as a prop. Here is an example:
|
15 |
|
16 | ```js
|
17 | var NavLink = require('flux-router-component').NavLink;
|
18 |
|
19 | var Nav = React.createClass({
|
20 | render: function () {
|
21 | // This example is using this.props.context for Nav and NavLink components.
|
22 | // You can also use the React context, as described in the Context section of this doc.
|
23 | var pages,
|
24 | links,
|
25 | context = this.props.context;
|
26 | pages = [
|
27 | {
|
28 | name: 'home',
|
29 | url: '/home',
|
30 | text: 'Home'
|
31 | },
|
32 | {
|
33 | name: 'about',
|
34 | url: '/about',
|
35 | text: 'About Us'
|
36 | }
|
37 | ];
|
38 | links = pages.map(function (page) {
|
39 | return (
|
40 | <li className="navItem">
|
41 | <NavLink href={page.url} context={context}>
|
42 | {page.text}
|
43 | </NavLink>
|
44 | </li>
|
45 | );
|
46 | });
|
47 | return (
|
48 | <ul className="nav">
|
49 | {links}
|
50 | </ul>
|
51 | );
|
52 |
|
53 | }
|
54 | });
|
55 | ```
|
56 |
|
57 | ### Example of Using `routeName` Property
|
58 |
|
59 | Before you continue with this example, you should know that you can always generate the url yourself outside of `NavLink` and pass it to `NavLink` as `href` prop just like the example above. Your code will be more straight-forward that way, and you will have more control over how to generate `href` (see more explanations in [the Example Usage section](#example-usage)).
|
60 |
|
61 | If you choose not to generate `href` yourself and the `context` prop you pass to `NavLink` provides `makePath(routeName, routeParams)`, you can also use the `routeName` prop (and the optional `navParams` prop). If the `href` prop is not present, `NavLink` will use `makePath(this.props.routeName, this.props.navParams)` from either `this.context` or `this.props.context` to generate the `href` for the anchor element. The `navParams` prop is useful for dynamic routes. It should be a hash object containing the route parameters and their values.
|
62 |
|
63 |
|
64 | Here is a quick example code showcasing how to use `routeName` prop along with `navParams` prop:
|
65 |
|
66 | ```js
|
67 | // assume routes are defined somewhere like this:
|
68 | // var routes = {
|
69 | // home: {
|
70 | // path: '/',
|
71 | // page: 'home'
|
72 | // },
|
73 | // article: {
|
74 | // path: '/article/:id',
|
75 | // page: 'article'
|
76 | // }
|
77 | // };
|
78 | var pages = [
|
79 | {
|
80 | routeName: 'home',
|
81 | text: 'Home'
|
82 | },
|
83 | {
|
84 | routeName: 'article',
|
85 | routeParams: {
|
86 | id: 'a'
|
87 | }
|
88 | text: 'Article A'
|
89 | }
|
90 | ];
|
91 | var Nav = React.createClass({
|
92 | render: function () {
|
93 | // context should provide executeAction() and makePath().
|
94 | // This example is using this.props.context for Nav and NavLink components.
|
95 | // You can also use the React context, as described in the Context section of this doc.
|
96 | var context = this.props.context;
|
97 | var links = pages.map(function (page) {
|
98 | return (
|
99 | <li className="navItem">
|
100 | <NavLink routeName={page.routeName} navParams={page.routeParams} context={context}>
|
101 | {page.text}
|
102 | </NavLink>
|
103 | </li>
|
104 | );
|
105 | });
|
106 | return (
|
107 | <ul className="nav">
|
108 | {links}
|
109 | </ul>
|
110 | );
|
111 | }
|
112 | });
|
113 | ```
|