Skip to content

Commit

Permalink
Client side routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Jul 7, 2024
1 parent 55a8591 commit 5914ecc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
26 changes: 25 additions & 1 deletion client/app.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
let home = () => Routes.(nil); // '/'
let hello = () => Routes.(s("hello") /? nil); // /hello

let homeRoute = Routes.(home() @--> (() => print_endline("in home page")));
let helloRoute = Routes.(hello() @--> (() => print_endline("in hello view")));

let routes = Routes.one_of([homeRoute, helloRoute]);

module Main = {
[@react.component]
let make = () => {
let target =
ReasonReactRouter.useUrl().path
|> String.concat("/");
switch (Routes.match'(routes, ~target)) {
| Routes.FullMatch(e) => e()
| Routes.MatchWithTrailingSlash(e) => e()
| Routes.NoMatch => ()
};
<Reason_india_website_js.App />;
}
};

switch (ReactDOM.querySelector("#root")) {
| Some(el) => ignore @@ ReactDOM.Client.hydrateRoot(el, <Reason_india_website_js.App />)
| Some(el) =>
ReactDOM.Client.createRoot(el)->ReactDOM.Client.render(<Main />)
| None => ()
};
2 changes: 1 addition & 1 deletion client/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(melange.emit
(alias client)
(target app)
(libraries melange reason_india_website_js reason-react)
(libraries routes melange reason_india_website_js reason-react)
(preprocess
(pps reason-react-ppx melange.ppx))
(module_systems commonjs))
4 changes: 3 additions & 1 deletion lib/js/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
(modes melange)
(libraries styled-ppx.css styled-ppx.emotion melange reason-react)
(preprocess
(pps styled-ppx melange.ppx reason-react-ppx)))
(pps
server-reason-react.browser_ppx -js
styled-ppx melange.ppx reason-react-ppx)))

(copy_files# "../native/*.re")
11 changes: 11 additions & 0 deletions lib/native/Link.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[@alert "-all--all+browser_only"];
let%browser_only handleLinkAnchorOnClick = (href, e) => {
React.Event.Mouse.preventDefault(e);
ReasonReactRouter.push(href);
};


[@react.component]
let make = (~href, ~children) => {
<a onClick={handleLinkAnchorOnClick(href)} href> {children} </a>;
};
3 changes: 3 additions & 0 deletions lib/native/app.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ module Container = [%styled.section

[@react.component]
let make = () => {
let hey = React.string("hey");
let (count, setCount) = useState(0);
<Container blah=`flex>
<Link href="/hello">{React.string("to hello")}</Link>
<section className="centered">
<p>{switch(true) { | true => hey | false => React.string("false")}}</p>
<section>
{React.string(" clicked " ++ Int.to_string(count) ++ " times")}
</section>
Expand Down
2 changes: 2 additions & 0 deletions lib/native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
(libraries
styled-ppx.emotion_native
styled-ppx.css_native
routes
server-reason-react.react
server-reason-react.reactDom)
(preprocess
(pps styled-ppx --native
server-reason-react.browser_ppx
server-reason-react.melange_ppx
server-reason-react.ppx)))

0 comments on commit 5914ecc

Please sign in to comment.