<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jacek Schæ</title>
    <description>The latest articles on Forem by Jacek Schæ (@jacekschae).</description>
    <link>https://forem.com/jacekschae</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F93813%2Fc7a1644b-b026-4aa7-b0bf-79d2ded7e547.jpeg</url>
      <title>Forem: Jacek Schæ</title>
      <link>https://forem.com/jacekschae</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jacekschae"/>
    <language>en</language>
    <item>
      <title>ClojureScript Loves NPM</title>
      <dc:creator>Jacek Schæ</dc:creator>
      <pubDate>Fri, 15 Feb 2019 11:37:09 +0000</pubDate>
      <link>https://forem.com/jacekschae/clojurescript-loves-npm-1ah4</link>
      <guid>https://forem.com/jacekschae/clojurescript-loves-npm-1ah4</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sHRxzvvO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eit14l44y7h0miwshopb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sHRxzvvO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eit14l44y7h0miwshopb.png" alt="Alt Clojure Script Loves React"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every languge that complies/transpiles to JS want to connect to npm to use this huge ecosystem. The master of this is of course Ecma Script. The second one - in my humble opition - is ClojureScript, due to &lt;a href="https://github.com/thheller/shadow-cljs"&gt;shadow-cljs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I know there is a lot of work going on in differnt communities that tap into npm. By all means I don't try to diminish any of that and state that CLJS (ClojureScript) is the best . I want to give you a glance how this works in ClojureScript.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing npm packages
&lt;/h2&gt;

&lt;p&gt;In ClojureScript we would install npm packages as we would in JavaScript. We would use standard package.json and shadow-cljs build tool would figure out the rest.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oJUU0Gpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pougazyazggtg9ovvmyb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oJUU0Gpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pougazyazggtg9ovvmyb.png" alt="Alt npm install"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the installation we would have to require the package. The require statment is almost identical to import from JS. In CLJS we swap the order - first we say from where and then what.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7hlxJSZs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bzwcbls8o2pr167iqixy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7hlxJSZs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bzwcbls8o2pr167iqixy.png" alt="Alt import firebase"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every ClojureScript file starts with a ns - namespace declaration. Following that we have &lt;code&gt;require&lt;/code&gt;, instead of &lt;code&gt;import&lt;/code&gt; . And then we are defining a function with &lt;code&gt;defn&lt;/code&gt; . This function will tap into our required firebase packages and instead of using &lt;code&gt;.&lt;/code&gt; to navigate to our method &lt;code&gt;initializeApp&lt;/code&gt; we are using &lt;code&gt;/&lt;/code&gt; . We make sure that when we invoke JS method &lt;code&gt;initializeApp&lt;/code&gt; we are converting CLJS map (datastructure) to JS Object with &lt;code&gt;#js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's try some other npm packages to get a better feeling for interop between npm and ClojureScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  React
&lt;/h2&gt;

&lt;p&gt;How about using React? ClojureScript has a couple of wrappers for React - the most popular one is &lt;a href="https://github.com/reagent-project/reagent"&gt;Reagent&lt;/a&gt;. Here is a simple counter example with React hooks and Reagent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qVwSTqBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hrp8behge6n8rwn06bt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qVwSTqBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hrp8behge6n8rwn06bt0.png" alt="Alt react-reagent"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In both examples we are first importing/requiring React and Reagent. Then we are defining state in React - using hooks and in Reagent using atoms.What follows is JSX (JavaScript) and hiccup (ClojureScript) component. This is cool, but how would we use React UI Libraries from Reagent?&lt;/p&gt;

&lt;h2&gt;
  
  
  React UI Libraries
&lt;/h2&gt;

&lt;p&gt;One of the most popular UI libraires is &lt;a href="https://material-ui.com/"&gt;material-ui&lt;/a&gt;. After the installation we require this library and then import our Button component as well as React. In Clojure Script we only require the Button. We don't need to require Reagent since it's in our ClojureScript deps. To interop with React we would use &lt;code&gt;:&amp;gt;&lt;/code&gt; form and pass all of the properties that we want in a &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--skuvm7Hj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lvat0jy7nsefgnxoara1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--skuvm7Hj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lvat0jy7nsefgnxoara1.png" alt="Alt material-ui-button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux
&lt;/h2&gt;

&lt;p&gt;How about Redux you ask? Well, there is a library that is build on top of Reagent, called &lt;a href="https://github.com/Day8/re-frame"&gt;re-frame&lt;/a&gt;. First designed in Dec 2014, it even pre-dates the official Elm Architecture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FoXaH-3N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xec0tvs3yn2cbkyxtftc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FoXaH-3N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xec0tvs3yn2cbkyxtftc.png" alt="Alt redux-re-frame"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By now you should have a pretty good picture why CLJS loves npm ecosystem and how easy it is to interop from CLJS to JS. Maybe this is interesting for you, and your are wondering why? Why should you even try ClojureScript?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Immutable
&lt;/h3&gt;

&lt;p&gt;All ClojureScritp data structures are immutable and presistant. You don't need to learn new API if you want to leaverge something like ImmutableJS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional
&lt;/h3&gt;

&lt;p&gt;ClojureScript embraces Functional Programming ideas at its core. You don't need Lodash or Ramda.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple
&lt;/h3&gt;

&lt;p&gt;With shadow-cljs you don't need to spend time configuring your builds. You require what you need and the build tool will do the job.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concise
&lt;/h3&gt;

&lt;p&gt;You liability is the LoC you write . ClojureScript is one of the most concies programming languges out there. Chek out the last section of &lt;a href="https://medium.freecodecamp.org/a-real-world-comparison-of-front-end-frameworks-with-benchmarks-2018-update-e5760fb4a962"&gt;this comparision&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Powerful
&lt;/h3&gt;

&lt;p&gt;ClojureScript uses Google Closure Tools for code minification and tree shaking. The same tolls that Google is using to build Gmail, Google Calendar, Google Docs, Google Maps.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;It compiles/transpiles to JavaScript. Just as ES (EcmaScript) ReasonML,PureScript, and Elm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Friendly
&lt;/h3&gt;

&lt;p&gt;ClojureScript community is the most friendly and welcoming group of people that I have ever met online. We mainly hang out on &lt;a href="https://clojurians.herokuapp.com/"&gt;Slack&lt;/a&gt; and &lt;a href="https://clojureverse.org/"&gt;ClojureVerse&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Full-Stack
&lt;/h3&gt;

&lt;p&gt;ClojureScript older brother - Clojure embraces all of these ideas with Java. If you want to write your server on one of the most performant and stable platform there is - Java Virtuel Machine - you can do that using the same language.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you like this article you should follow me on &lt;a href="https://twitter.com/JacekSchae"&gt;Twitter&lt;/a&gt;. I only write/tweet about programming and technology - mainly about ClojureScript and Clojure.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>clojure</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Learn how to build Functional Front-ends with ClojureScript and React.</title>
      <dc:creator>Jacek Schæ</dc:creator>
      <pubDate>Sun, 30 Sep 2018 18:12:53 +0000</pubDate>
      <link>https://forem.com/jacekschae/learn-how-to-build-functional-front-ends-with-clojurescript-and-react-58oc</link>
      <guid>https://forem.com/jacekschae/learn-how-to-build-functional-front-ends-with-clojurescript-and-react-58oc</guid>
      <description>&lt;p&gt;You probably heard about React, and did you hear about Reagent? Reagent is a minimalistic interface between ClojureScript and React.&lt;/p&gt;

&lt;p&gt;ClojureScript, just as Elm, ReasonML and EcmaScript compile/transpile to JavaScript.&lt;/p&gt;

&lt;p&gt;With Reagent you can define components using nothing else but plain ClojureScript functions and data structures.&lt;/p&gt;

&lt;p&gt;Let’s take a look at a couple examples and see how they compare. This is not a rant on React. React is awesome! Nor a rant on JS. JS is awesome too! This is to show how React looks like in a language with immutable and persistent data structures.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Stateless Component
&lt;/h2&gt;

&lt;p&gt;The simplest component in Reagent and in React is just a function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MkVE_VFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/tcmokg12qhoh1kbmhhaa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MkVE_VFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/tcmokg12qhoh1kbmhhaa.png" alt="alt stateless component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very often in JavaScript you would see them as &lt;code&gt;const&lt;/code&gt; one liners:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HelloMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A Statefull Component
&lt;/h2&gt;

&lt;p&gt;Since Reagent is build on top of CLJS (ClojureScript) immutable data structures there is no need for extra libraries (Redux, MobX) or syntax (&lt;code&gt;this.setState&lt;/code&gt;) to work with your state. In CLJS you would use &lt;code&gt;atoms&lt;/code&gt; (reference type in CLJS) for dealing with your state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QLl71Gla--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pbawqtekf05177d2sq0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QLl71Gla--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pbawqtekf05177d2sq0e.png" alt="alt statefull component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example we are creating a &lt;code&gt;let&lt;/code&gt; binding for an &lt;code&gt;atom&lt;/code&gt; — inside our function - &lt;code&gt;defn&lt;/code&gt;. The &lt;code&gt;let&lt;/code&gt; binding is available only inside the scope of the function and then we are &lt;code&gt;swap!&lt;/code&gt;-ing the value with &lt;code&gt;on-click&lt;/code&gt; by using &lt;code&gt;inc&lt;/code&gt; (increment) function.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Class Component with Life Cycle Methods
&lt;/h2&gt;

&lt;p&gt;This example shows a class component with life cycle methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S62BpnZu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r5pfooe1bluljnqgogge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S62BpnZu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r5pfooe1bluljnqgogge.png" alt="alt class component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Reagent we would use them less than 1%. The reason for that are — atoms — they will keep track when they should be updated. In Reagent we could write this component, without a need for &lt;code&gt;componentDidMount&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;r/atom&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;js/setInterval&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;swap!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;:div&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="s"&gt;"Seconds: "&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;])))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Want to learn more?
&lt;/h2&gt;

&lt;p&gt;If you would like to learn more about ClojureScript and Reagent try this &lt;a href="https://www.learnreagent.com/"&gt;FREE&lt;/a&gt; video course and build GIGGIN app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  More information about the course at &lt;a href="https://www.learnreagent.com/"&gt;learnreagent.com&lt;/a&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;




&lt;p&gt;And if you like this article you should follow me on DEV and &lt;a href="https://twitter.com/JacekSchae"&gt;Twitter&lt;/a&gt; I only write/tweet about programming and technology.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>clojure</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
