@colisweb/rescript-toolkit 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "scripts": {
5
5
  "clean": "rescript clean",
6
6
  "build": "rescript build",
@@ -5,16 +5,21 @@ module type RouterConfig = {
5
5
  }
6
6
 
7
7
  module Make = (Config: RouterConfig) => {
8
+ type contextState = {
9
+ currentRoute: Config.t,
10
+ previousRoute: option<Config.t>,
11
+ }
8
12
  let navigate = (route: Config.t) => RescriptReactRouter.push(route->Config.toString)
9
13
 
10
14
  let replace = (route: Config.t) => RescriptReactRouter.replace(route->Config.toString)
11
15
 
12
- let routerContext = React.createContext(
13
- RescriptReactRouter.dangerouslyGetInitialUrl()->Config.make,
14
- )
16
+ let routerContext = React.createContext({
17
+ currentRoute: RescriptReactRouter.dangerouslyGetInitialUrl()->Config.make,
18
+ previousRoute: None,
19
+ })
15
20
 
16
21
  module RouterContextProvider = {
17
- let makeProps = (~value: Config.t, ~children, ()) =>
22
+ let makeProps = (~value: contextState, ~children, ()) =>
18
23
  {
19
24
  "value": value,
20
25
  "children": children,
@@ -30,6 +35,8 @@ module Make = (Config: RouterConfig) => {
30
35
  RescriptReactRouter.dangerouslyGetInitialUrl()->Config.make
31
36
  )
32
37
 
38
+ let previousRoute = Toolkit__Hooks.usePrevious(currentRoute)
39
+
33
40
  React.useLayoutEffect1(() => {
34
41
  let watcherID = RescriptReactRouter.watchUrl(url =>
35
42
  setCurrentRoute(_ => url |> Config.make)
@@ -37,7 +44,14 @@ module Make = (Config: RouterConfig) => {
37
44
  Some(() => RescriptReactRouter.unwatchUrl(watcherID))
38
45
  }, [setCurrentRoute])
39
46
 
40
- <RouterContextProvider value=currentRoute> {children(~currentRoute)} </RouterContextProvider>
47
+ let contextValue: contextState = {
48
+ previousRoute: previousRoute,
49
+ currentRoute: currentRoute,
50
+ }
51
+
52
+ <RouterContextProvider value={contextValue}>
53
+ {children(~currentRoute)}
54
+ </RouterContextProvider>
41
55
  }
42
56
  }
43
57
 
@@ -47,7 +61,7 @@ module Make = (Config: RouterConfig) => {
47
61
  routeA->Config.toString === routeB->Config.toString
48
62
 
49
63
  let useIsCurrentRoute = (route: Config.t): bool => {
50
- let currentRoute = React.useContext(routerContext)
64
+ let {currentRoute} = React.useContext(routerContext)
51
65
  isRouteEqual(currentRoute, route)
52
66
  }
53
67