forked from molefrog/wouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (130 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import makeHistory from "./history.js";
import makeMatcher from "./matcher.js";
import {
useRef,
useMemo,
useEffect,
useState,
useContext,
useCallback,
createContext,
isValidElement,
cloneElement,
createElement as h,
Children
} from "react";
/*
* Part 1, Hooks API: useRouter, useRoute and useLocation
*/
const RouterCtx = createContext();
export const buildRouter = (options = {}) => {
return {
history: options.history || makeHistory(),
matcher: options.matcher || makeMatcher()
};
};
export const useRouter = () => {
const providedRouter = useContext(RouterCtx);
// either obtain the router from the outer context
// (provided by the `<Router /> component) or create
// a default one on demand.
const router = useMemo(
() => (providedRouter ? providedRouter : buildRouter()),
[providedRouter]
);
return router;
};
export const useLocation = () => {
const history = useRouter().history;
const [location, update] = useState(history.path());
// subscribe to history updates
useEffect(() => history.subscribe(x => update(x)), [history]);
const pushLocation = useCallback(y => history.push(y), [history]);
return [location, pushLocation];
};
export const useRoute = pattern => {
const router = useRouter();
const [path] = useLocation();
return router.matcher(pattern, path);
};
/*
* Part 2, Low Carb Router API: Router, Route, Link, Switch
*/
export const Router = props => {
const ref = useRef(null);
// this little trick allows to avoid having unnecessary
// calls to potentially expensive `buildRouter` method.
// https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
function getRouter() {
if (ref.current !== null) {
return ref.current;
} else {
return (ref.current = buildRouter(props));
}
}
return h(RouterCtx.Provider, {
value: getRouter(),
children: props.children
});
};
export const Route = props => {
const { children, path } = props;
const [matches, params] = useRoute(props.path);
if (!matches && !props.match) {
return null;
}
// React-Router style `component` prop
if (props.component) {
return h(props.component, { params: params });
}
// support render prop or plain children
return typeof children === "function" ? children(params) : children;
};
export const Link = props => {
const href = props.href || props.to;
const child = props.children;
const [, navigate] = useLocation();
const onClick = useCallback(
event => {
event.preventDefault();
navigate(href);
if (props.onClick) {
props.onClick();
}
},
[href, props.onClick]
);
// wraps children in `a` if needed
const extraProps = { href, onClick, to: null };
const jsx = isValidElement(child) ? child : h("a", props);
return cloneElement(jsx, extraProps);
};
export const NavLink = props => {
if (typeof props.children !== 'function') {
throw Error('children should be a function')
}
const href = props.href || props.to;
const [match] = useRoute(href);
const child = props.children(match);
return Link({ ...props, children: child });
}
export const Switch = ({ children, location }) => {
const { matcher } = useRouter();
const [originalLocation] = useLocation();
let element = null;
// this looks similar to Switch implementation from React Router
// https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Switch.js
Children.forEach(children, child => {
if (!element && isValidElement(child)) {
const [match] = matcher(child.props.path, location || originalLocation);
if (match) element = child;
}
});
return element ? cloneElement(element, { match: true }) : null;
};
export const Redirect = props => {
const router = useRouter();
useEffect(() => router.history.push(prop.href || props.to));
return null;
};
export default useRoute;