Monday , June 24 2019
Home / Uncategorized / Find out how to create functional front-ends with ClojureScript and React.

Find out how to create functional front-ends with ClojureScript and React.



You've probably heard of React and heard of Reagent? Reagent is a minimalist interface between ClojureScript and React.

ClojureScript, just like Elm, ReasonML and EcmaScript compile / transpose in JavaScript.

With Reagent, you can define components using nothing but simple ClojureScript data functions and structures.

Let's take a look at a couple of examples and see how they compare. This is not an outlet on React. React is fantastic! Nor an outburst on JS. Even JS is fantastic! This is to show how React appears in a language with immutable and persistent data structures.

A stateless component

The simplest component in Reagent and React is only a function.

stateless component alt

Very often in JavaScript you would see them as const a lining:

const HelloMessage = scene objects => <div>Hello {scene objects.first name}</div>;

A Statefull component

Because Reagent is built on immutable CLJS (ClojureScript) data structures, no additional libraries (Redux, MobX) or syntax are needed (this.setState) to work with your state. In CLJS you would use atoms (reference type in CLJS) to deal with your state.

statefull component alt

In this example we are creating a to allow binding for one atom– inside our function – defn . The to allowbinding is available only in the scope of the function and therefore we are to exchange! – setting the value with at the click using inc(increase).

A class component with life cycle methods

This example shows a class component with life cycle methods.

component of alt class

In Reagent we would use them less than 1%. The reason for this is – the atoms – that will keep track when they should be updated. In Reagent we could write this component, without necessity componentDidMount:

(defn Timer []
  (to allow [[[[seconds (r / atom 0)]
    (fn []
      (js / setInterval #(to exchange! seconds inc) 1000)
      [[[[: div
       "Seconds" @seconds])))

Would you like to know more?

If you want to learn more about ClojureScript and Reagent, try this FREE video course and create the GIGGIN app.

More information on the course on learnreagent.com


And if you like this article you should follow me on DEV e chirping I write / tweet only about programming and technology.

dev.to is the place where software developers stay in the circuit and avoid career stagnation.
Registering (for free!) Is the first step.




Source link

Leave a Reply

Your email address will not be published.