Gathering detailed insights and metrics for @dck/reason-react-navigation
Gathering detailed insights and metrics for @dck/reason-react-navigation
a fast, declarative microrouter for rescript based on reroute works
npm install @dck/reason-react-navigation
Typescript
Module System
Node Version
NPM Version
ReScript (85.52%)
JavaScript (14.48%)
Total Downloads
3,263
Last Day
1
Last Week
3
Last Month
24
Last Year
259
7 Stars
44 Commits
1 Watching
1 Branches
2 Contributors
Latest Version
1.3.2
Package Id
@dck/reason-react-navigation@1.3.2
Unpacked Size
6.49 kB
Size
2.16 kB
File Count
4
NPM Version
6.11.3
Node Version
10.16.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-76.9%
3
Compared to previous week
Last month
14.3%
24
Compared to previous month
Last year
-46.7%
259
Compared to previous year
3
2
This is project is based on the great old reroute module. It's just using the latest Reason React API (hooks & context).
Install the module :
1$ yarn add @dck/reason-react-navigation
Then add it to your bsconfig.json
:
1{ 2 "bs-dependencies": ["reason-react", "@dck/reason-react-navigation"] 3}
1open ReasonReactNavigation;
2
3module RouterConfig = {
4 type route =
5 | Home
6 | Hello(string)
7 | NotFound;
8
9 let routeFromUrl = (url: ReasonReact.Router.url) =>
10 switch (url.path) {
11 | [] => Home
12 | ["hello", name] => Hello(name)
13 | ["404"]
14 | _ => NotFound
15 };
16
17 let routeToUrl = (route: route) =>
18 switch (route) {
19 | Home => "/"
20 | Hello(name) => "/hello/" ++ name
21 | NotFound => "/404"
22 };
23};
24
25module Router = CreateRouter(RouterConfig);
26
27[@react.component]
28let make = () =>
29 <div>
30 <a href="#" onClick={e => {
31 event->ReactEvent.Synthetic.preventDefault;
32 Router.navigate(RouterConfig.Home)
33 }}>
34 "Home "->React.string
35 </a>
36
37 <a href="#" onClick={e => {
38 event->ReactEvent.Synthetic.preventDefault;
39 Router.navigate(RouterConfig.Hello("dck"))
40 }}>
41 "Hello DCK "->React.string
42 </a>
43 <Router.Provider>
44 ...{
45 (~currentRoute) =>
46 <p>
47 {
48 (
49 switch (currentRoute) {
50 | RouterConfig.Home => "This is home"
51 | RouterConfig.Hello(n) => "Hi " ++ n
52 | _ => "404 not found"
53 }
54 )
55 |> React.string
56 }
57 </p>
58 }
59 </Router.Provider>
60 </div>
Quick and dirty implementation :
1open ReasonReactNavigation; 2 3module Config = { 4 type route = 5 | Login 6 | Dashboard 7 | NotFound; 8 9 let routeToUrl = route => 10 switch (route) { 11 | Login => "/login" 12 | Dashboard => "/dashboard" 13 | NotFound => "/404" 14 }; 15 let routeFromUrl = (url: ReasonReact.Router.url) => 16 switch (url.path) { 17 | ["login"] => Login 18 | ["dashboard"] => Dashboard 19 | _ => NotFound 20 }; 21}; 22 23module Router = CreateRouter(Config); 24 25[@react.component] 26let make = () => { 27 let (currentUser, setCurrentUser) = React.useState(() => None); 28 29 <Router.Provider> 30 { 31 (~currentRoute) => 32 switch (currentRoute, currentUser) { 33 | (Config.Login, None) => <Login /> 34 | (Config.Login, Some(_)) => <Redirect to_=Config.Dashboard /> 35 | (route, Some(user)) => 36 switch (route) { 37 | Config.Dashboard => <Dashboard user /> 38 | _ => <NotFound /> 39 } 40 | (Config.NotFound, _) => <NotFound /> 41 | (_, None) => <Redirect to_=Login /> 42 } 43 } 44 </Router.Provider> 45};
No vulnerabilities found.
No security vulnerabilities found.