You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add Hooks APIs
* Documenation & change useMatch response to match Match
* add hook API navigation links
* some hooks documentation in the intro
* address comments
* yarn format
"useLocation hook was used but a LocationContext.Provider was not found in the parent tree. Make sure this is used in a component that is a child of Router"
532
+
);
533
+
}
534
+
535
+
returncontext.location;
536
+
};
537
+
538
+
constuseNavigate=()=>{
539
+
constcontext=useContext(LocationContext);
540
+
541
+
returncontext.navigate;
542
+
};
543
+
544
+
constuseParams=()=>{
545
+
const{ basepath }=useContext(BaseContext);
546
+
constlocation=useLocation();
547
+
548
+
constresults=match(basepath,location.pathname);
549
+
550
+
returnresults ? results.params : null;
551
+
};
552
+
553
+
constuseRouterMatch=path=>{
554
+
if(!path){
555
+
thrownewError(
556
+
"useRouterMatch(path: string) requires an argument of a string to match against"
it("throws an error if a location context hasnt been rendered",()=>{
903
+
functionFixture(){
904
+
constlocation=useLocation();
905
+
return`path: ${location.pathname}`;
906
+
}
907
+
908
+
expect(()=>{
909
+
renderToString(<Fixture/>);
910
+
}).toThrow(
911
+
"useLocation hook was used but a LocationContext.Provider was not found in the parent tree. Make sure this is used in a component that is a child of Router"
Matches a path to the location. Matching is relative to any parent Routers, but not parent match's, because they render even if they don't match.
4
+
5
+
This API requires a hook-compatible version of React.
6
+
7
+
```jsx
8
+
import { useMatch } from"@reach/router"
9
+
10
+
constApp= () => {
11
+
constmatch=useMatch('/hot/:item');
12
+
13
+
return match ? (
14
+
<div>Hot {match.item}</div>
15
+
) : (
16
+
<div>Uncool</div>
17
+
)
18
+
)
19
+
```
20
+
21
+
`useMatch` will return `null` if your path does not match the location. If it does match it will contain:
22
+
23
+
- `uri`
24
+
- `path`
25
+
- `:params`
26
+
27
+
## match\[param\]: string
28
+
29
+
Any params in your the path will be parsed and passed as `match[param]` to your callback.
30
+
31
+
```jsx
32
+
constmatch=useMatch("events/:eventId")
33
+
34
+
props.match?props.match.eventId:"No match"
35
+
```
36
+
37
+
## match.uri: string
38
+
39
+
The portion of the URI that matched. If you pass a wildcard path, the wildcard portion will not be included. Not sure how this is useful for a `Match`, but it's critical for how focus managment works, so we might as well pass it on to Match if we pass it on to Route Components!
0 commit comments