Skip to content

Reagent Intro

By Brian Sunter

Posted in Programming, Functional, Frontend, Clojure

Intro to Clojurescript with Reagent

The Javascript community has finally started rallying around the power of functional programming and immutability with React, but what would this look like in a language that supported these ideas really well?

What is Reagent?

Reagent is a Clojurescript wrapper around React. It simplifies the interface to React and allows us to define our UI using Clojure data structures, instead of JSX.

Hello World Component

Let's start by just getting something on the screen. Define a simple Reagent (React) component. Components are functions that take 0 or more arguments and return a hiccup data structure.

;; REPL function syntax for importing.
;; From a file, the syntax is:
;; (ns my-ns.core
;; (:require [reagent.core :as r]))
(require '[reagent.core :as r])

(defn hello [name]
[:p (str "Hello " name "!")])

(r/render-component [hello "World"] (.getElementById js/document "hello-world"))
 <div id="hello-world">Loading...</div>

Clojure functions and hiccup

defn defines a function in Clojure. Our function takes one name parameter and returns a vector. The last "expression" in the function is the return value.

(defn hello [name]
[:p (str "Hello " name "!")])

Calling our Function

The first thing in a set of parens is a function that will be called with the following expressions as arguments. Our function will return an array [:p "Hello World!"] with a :keyword as the first item signifying the type of element to be rendered, and the content of the element in the second position.

(hello "World")

Some functions can take any number of arguments, like str

(str "this" "function" "can" "take" "many" "args")


Keywords are named values commonly used in map keys. They are symbols prefixed with a : like :my-keyword or :h1.

Vectors and Map Syntax in Clojure

Vector Syntax []

The syntax for a vector is

[1 2 "three" "four"]

The elements are separated by whitespace in between brackets. , is treated as whitespace.

Map Syntax {}

The syntax for a map is

{:key-1 "value" :key-2 "value"}

Maps are a series of pairs in between braces.


Hiccup is a syntax for writing UIs with data. It is just a vector of vectors. The first item in the vector can be a :keyword, which will be rendered to a <div></div>.

(defn my-title
[:h1 title]])

[my-title "My Title"]

We can combine components, where a function that returns a component is the first element of the vector.

(defn my-app
[my-title "My Title"]
[:p "This is starting to get cool"]])

There is a shorthand for applying css classes and ids on elements.

[ "foo"]

:keyword components can optionally take a first argument of a map, in the form {:key "value"}

which will be rendered as <div key="value"></>

[:a {:href ""} "Link to google"]

State Management

For simple state management we can declare an atom. Whenever an atom is updated, all views referencing the atom with re-render with the latest data.

(ns counter
(:require [reagent.core :as r]))

(def click-count (r/atom 0))

(defn counter-component []
"Times Clicked: " @click-count ". "
[:input {:type "button" :value "Click Here"
:on-click #(swap! click-count inc)}]])
Brian Sunter
Brian Sunter

Whatever problem you have, I will solve it.