<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>clojurescript on C'est la Z</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/categories/clojurescript/</link><description>C'est la Z (clojurescript)</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 15 Jan 2020 09:37:17 -0400</lastBuildDate><atom:link href="https://cestlaz.github.io/cestlaz-old-hugo.github.io/categories/clojurescript/rss.xml" rel="self" type="application/rss"/><item><title>Clojurescript - building a simple router</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/post/clojurescript-router/</link><pubDate>Wed, 15 Jan 2020 09:37:17 -0400</pubDate><guid>https://cestlaz.github.io/cestlaz-old-hugo.github.io/post/clojurescript-router/</guid><description>&lt;p>
In this video we&amp;#39;ll look at creating a small router. &lt;/p>
&lt;p>
I originally looked at using one of the available libraries to set up
routing. I considered &lt;a href="https://github.com/metosin/reitit">reitit&lt;/a> and &lt;a href="https://github.com/juxt/bidi">bidi&lt;/a> but in the end decided to figure
out how to do it myself. &lt;/p>
&lt;p>
It turns out that whenever you change the hash part of a url -
everything following a # at the end of a url you get a &lt;code>hashchange&lt;/code>
event. You can handle that with&lt;/p>
&lt;p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-clojure" data-lang="clojure">&lt;span style="display:flex;">&lt;span> (&lt;span style="color:#a6e22e">.addEventListener&lt;/span> js/window &lt;span style="color:#e6db74">&amp;#34;hashchange&amp;#34;&lt;/span> &lt;span style="color:#f92672">#&lt;/span>(&lt;span style="color:#a6e22e">handleroutes&lt;/span> routes %))&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/p>
&lt;p>
where &lt;code>handleroutes&lt;/code> is your function to actually handle the routes.&lt;/p>
&lt;p>
You can then get the actual value of that last hash part of the url
with:&lt;/p>
&lt;p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-clojure" data-lang="clojure">&lt;span style="display:flex;">&lt;span>(&lt;span style="color:#a6e22e">.-location.hash&lt;/span> js/window)&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/p>
&lt;p>
Once you map that to whatever function you want you update the history
and mount the new page:&lt;/p>
&lt;p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-clojure" data-lang="clojure">&lt;span style="display:flex;">&lt;span> (&lt;span style="color:#a6e22e">.history.replaceState&lt;/span> js/window {} nil loc)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> (&lt;span style="color:#a6e22e">mount&lt;/span> newpage) &lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/p>
&lt;p>
All the code is up on GitHub: &lt;a href="https://github.com/zamansky/shadow-cljs-demo">&lt;a href="https://github.com/zamansky/shadow-cljs-demo">https://github.com/zamansky/shadow-cljs-demo&lt;/a>&lt;/a>&lt;/p>
&lt;p>
There are still things I need to figure out. Specifically, how can I
route without the #. That is, I can do it, but the page reloads every
time rather than just remounting the component I want. &lt;/p>
&lt;p>
The video goes through the entire process. Enjoy.&lt;/p>
&lt;p>
&amp;lt;iframe width=&amp;#34;560&amp;#34; height=&amp;#34;315&amp;#34;
src=&amp;#34;&lt;a href="https://www.youtube.com/embed/mbUHkfH6B9g">https://www.youtube.com/embed/mbUHkfH6B9g&lt;/a>&amp;#34; frameborder=&amp;#34;0&amp;#34;
allow=&amp;#34;accelerometer; autoplay; encrypted-media; gyroscope;
picture-in-picture&amp;#34; allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;&lt;/p></description></item></channel></rss>