Skip to content

Commit 56fbcd8

Browse files
committed
Add "Active Matching & Links" section to the README
1 parent 755a5e0 commit 56fbcd8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,65 @@ import AsyncRoute from 'preact-async-route';
7373
</Router>
7474
```
7575

76+
### Active Matching & Links
77+
78+
`preact-router` includes an add-on module called `match` that lets you wire your components up to Router changes.
79+
80+
Here's a demo of `<Match>`, which invokes the function you pass it (as its only child) in response to any routing:
81+
82+
```js
83+
import Router from 'preact-router';
84+
import Match from 'preact-router/match';
85+
86+
render(
87+
<div>
88+
<Match path="/">
89+
{ ({ matches, path, url }) => (
90+
<pre>{url}</pre>
91+
) }
92+
</Match>
93+
<Router>
94+
<div default>demo fallback route</div>
95+
</Router>
96+
</div>
97+
)
98+
99+
// another example: render only if at a given URL:
100+
101+
render(
102+
<div>
103+
<Match path="/">
104+
{ ({ matches }) => matches && (
105+
<h1>You are Home!</h1>
106+
) }
107+
</Match>
108+
<Router />
109+
</div>
110+
)
111+
```
112+
113+
`<Link>` is just a normal link, but it automatically adds and removes an "active" classname to itself based on whether it matches the current URL.
114+
115+
```js
116+
import { Router } from 'preact-router';
117+
import { Link } from 'preact-router/match';
118+
119+
render(
120+
<div>
121+
<nav>
122+
<Link activeClassName="active" href="/">Home</Link>
123+
<Link activeClassName="active" href="/foo">Foo</Link>
124+
<Link activeClassName="active" href="/bar">Bar</Link>
125+
</nav>
126+
<Router>
127+
<div default>
128+
this is a demo route that always matches
129+
</div>
130+
</Router>
131+
</div>
132+
)
133+
```
134+
76135
---
77136

78137

0 commit comments

Comments
 (0)