Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Refactor to Hook Api with backwards-compatible wrappers #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"refmt": 3,
"bs-dependencies": ["reason-react"],
"reason": {
"react-jsx": 2
"react-jsx": 3
},
"namespace": false,
"sources": [
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reason-reroute",
"version": "0.0.4",
"version": "0.0.5",
"description": "Tiny wrapper around ReasonReact.Router ",
"main": "index.js",
"scripts": {
Expand All @@ -27,7 +27,7 @@
},
"homepage": "https://github.com/callstack/reroute#readme",
"devDependencies": {
"bs-platform": "^4.0.3",
"reason-react": "^0.5.3"
"bs-platform": "^5.0.4",
"reason-react": "^0.7.0"
}
}
96 changes: 62 additions & 34 deletions src/ReRoute.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,74 @@ module type RouterConfig = {
};

module CreateRouter = (Config: RouterConfig) => {
module Container = {
type action =
| ChangeRoute(Config.route);
type state = Config.route;
let component = ReasonReact.reducerComponent("CallstackRerouteRouter");
let make = children => {
...component,
initialState: () =>
ReasonReact.Router.dangerouslyGetInitialUrl() |> Config.routeFromUrl,
reducer: (action, _state) =>
switch (action) {
| ChangeRoute(route) => ReasonReact.Update(route)
},
didMount: self => {
let useRouter = () => {
let (currentRoute: Config.route, setCurrentRoute) =
React.useState(() =>
ReasonReact.Router.dangerouslyGetInitialUrl() |> Config.routeFromUrl
);
React.useEffect1(
() => {
let watcherID =
ReasonReact.Router.watchUrl(url =>
self.send(ChangeRoute(url |> Config.routeFromUrl))
setCurrentRoute(_old => url |> Config.routeFromUrl)
);
self.onUnmount(() => ReasonReact.Router.unwatchUrl(watcherID));
Some(() => ReasonReact.Router.unwatchUrl(watcherID));
},
render: self => children(~currentRoute=self.state),
[||],
);
currentRoute;
};

module Container = {
[@react.component]
let make = (~children) => {
let currentRoute = useRouter();
children(~currentRoute);
};

module Jsx2 = {
let component = ReasonReact.statelessComponent("RouterContainer");

let make = children =>
ReasonReactCompat.wrapReactForReasonReact(
make,
makeProps(~children, ()),
children,
);
};
};
module Link = {
let component = ReasonReact.statelessComponent("CallstackRerouteLink");
let make = (~route, children) => {
...component,
render: _self => {
let href = Config.routeToUrl(route);
<a
href
onClick=(
event => {
event->ReactEvent.Synthetic.preventDefault;
ReasonReact.Router.push(href);
}
)>
(ReasonReact.array(children))
</a>;
},
[@react.component]
let make = (~route, ~className=?, ~children) => {
let href = Config.routeToUrl(route);
<a
href
?className
onClick={
event => {
event->ReactEvent.Synthetic.preventDefault;
ReasonReact.Router.push(href);
}
}>
children
</a>;
};

module Jsx2 = {
let component = ReasonReact.statelessComponent("Link");

let make =
(~route, ~className=?, children: array(ReasonReact.reactElement)) =>
ReasonReactCompat.wrapReactForReasonReact(
make,
makeProps(
~route,
~className?,
~children=React.array(children),
(),
),
React.array(children),
);
};
};
};
};