reagent/README.md

170 lines
5.3 KiB
Markdown
Raw Permalink Normal View History

2013-12-19 12:11:02 +00:00
2014-01-17 10:12:11 +00:00
# Reagent
2013-12-19 12:11:02 +00:00
A simple [ClojureScript](http://github.com/clojure/clojurescript) interface to [React](http://facebook.github.io/react/).
Reagent provides a way to write efficient React components using (almost) nothing but plain ClojureScript functions.
* **[Detailed intro with live examples](http://reagent-project.github.io/)**
* **[News](http://reagent-project.github.io/news/index.html)**
* **[API Documentation](http://reagent-project.github.io/docs/master/)**
2018-03-14 21:10:45 +00:00
* **[Tutorials and FAQ](https://github.com/reagent-project/reagent/tree/master/docs)**
2017-05-15 07:41:12 +00:00
* **Community discussion and support channels**
* **[#reagent](https://clojurians.slack.com/messages/reagent/)** channel in [Clojure Slack](http://clojurians.net/)
* **[Reagent Project Mailing List](https://groups.google.com/forum/#!forum/reagent-project)**
2013-12-19 12:11:02 +00:00
2016-02-28 01:58:20 +00:00
### Prerequisites
* Java JDK
2016-02-28 01:58:20 +00:00
* [Leiningen](http://leiningen.org/)
### Usage
To create a new Reagent project simply run:
lein new reagent myproject
2016-02-16 23:32:36 +00:00
If you wish to only create the assets for ClojureScript without a Clojure backend then do the following instead:
lein new reagent-frontend myproject
This will setup a new Reagent project with some reasonable defaults, see here for more [details](https://github.com/reagent-project/reagent-template).
To use Reagent in an existing project you add this to your dependencies in `project.clj`:
2013-12-19 13:06:49 +00:00
2016-10-26 00:08:54 +00:00
[![Clojars Project](http://clojars.org/reagent/latest-version.svg)](http://clojars.org/reagent) <br>
2016-10-26 00:08:05 +00:00
[![CircleCI](https://circleci.com/gh/reagent-project/reagent.svg?style=svg)](https://circleci.com/gh/reagent-project/reagent)
2014-01-10 12:59:38 +00:00
This is all you need to do if you want the standard version of React. If you want the version of React with addons, you'd use something like this instead:
2014-01-10 12:59:38 +00:00
[reagent "0.7.0" :exclusions [cljsjs/react]]
2017-03-19 14:49:30 +00:00
[cljsjs/react-with-addons "15.4.2-2"]
2014-01-10 12:59:38 +00:00
If you want to use your own build of React (or React from a CDN), you have to use `:exclusions` variant of the dependency, and also provide a file named "cljsjs/react.cljs", containing just `(ns cljsjs.react)`, in your project.
2014-01-10 12:59:38 +00:00
2013-12-19 13:06:49 +00:00
2013-12-19 12:11:02 +00:00
## Examples
2014-01-17 10:12:11 +00:00
Reagent uses [Hiccup-like](https://github.com/weavejester/hiccup) markup instead of React's sort-of html. It looks like this:
2013-12-19 12:11:02 +00:00
```clj
(defn some-component []
[:div
[:h3 "I am a component!"]
[:p.someclass
"I have " [:strong "bold"]
[:span {:style {:color "red"}} " and red"]
" text."]])
```
Reagent extends standard Hiccup in one way: it is possible to "squeeze" elements together by using a `>` character.
```clj
[:div
[:p
[:b "Nested Element"]]]
```
can be written as:
```clj
[:div>p>b "Nested Element"]
```
2018-01-08 16:36:18 +00:00
> **Since version 0.8:** The `:class` attribute also supports collections of classes, and nil values are removed:
>
> ```clj
> [:div {:class ["a-class" (when active? "active") "b-class"]}]
> ```
You can use one component inside another:
2013-12-19 12:11:02 +00:00
```clj
(defn calling-component []
[:div "Parent component"
[some-component]])
```
And pass properties from one component to another:
```clj
2014-02-21 16:16:26 +00:00
(defn child [name]
[:p "Hi, I am " name])
2013-12-19 12:11:02 +00:00
(defn childcaller []
2014-02-21 16:16:26 +00:00
[child "Foo Bar"])
2013-12-19 12:11:02 +00:00
```
You mount the component into the DOM like this:
```clj
(defn mountit []
(r/render [childcaller]
(.-body js/document)))
2013-12-19 12:11:02 +00:00
```
2014-01-17 10:12:11 +00:00
assuming we have imported Reagent like this:
2013-12-19 12:11:02 +00:00
```clj
(ns example
(:require [reagent.core :as r]))
2013-12-19 12:11:02 +00:00
```
2014-01-17 10:12:11 +00:00
State is handled using Reagent's version of `atom`, like this:
2013-12-19 12:11:02 +00:00
```clj
(defonce click-count (r/atom 0))
2013-12-19 12:11:02 +00:00
(defn state-ful-with-atom []
[:div {:on-click #(swap! click-count inc)}
"I have been clicked " @click-count " times."])
```
2014-01-17 10:12:11 +00:00
Any component that dereferences a `reagent.core/atom` will be automatically re-rendered.
2013-12-19 12:11:02 +00:00
If you want do some setting up when the component is first created, the component function can return a new function that will be called to do the actual rendering:
```clj
(defn timer-component []
(let [seconds-elapsed (r/atom 0)]
(fn []
(js/setTimeout #(swap! seconds-elapsed inc) 1000)
[:div
"Seconds Elapsed: " @seconds-elapsed])))
2013-12-19 12:11:02 +00:00
```
This way you can avoid using React's lifecycle callbacks like `getInitialState` and `componentWillMount` most of the time.
2014-01-17 10:12:11 +00:00
But you can still use them if you want to, either using `reagent.core/create-class` or by attaching meta-data to a component function:
2013-12-19 12:11:02 +00:00
```clj
(defonce my-html (r/atom ""))
2014-02-21 16:16:26 +00:00
(defn plain-component []
[:p "My html is " @my-html])
2013-12-19 12:11:02 +00:00
(def component-with-callback
(with-meta plain-component
{:component-did-mount
(fn [this]
2014-01-17 10:12:11 +00:00
(reset! my-html (.-innerHTML (reagent/dom-node this))))}))
2013-12-19 12:11:02 +00:00
```
See the examples directory for more examples.
## Performance
2014-01-17 10:12:11 +00:00
React is pretty darn fast, and so is Reagent. It should even be faster than plain old javascript React a lot of the time, since ClojureScript allows us to skip a lot of unnecessary rendering (through judicious use of React's `shouldComponentUpdate`).
2013-12-19 12:11:02 +00:00
The ClojureScript overhead is kept down, thanks to lots of caching.
2018-02-21 02:04:24 +00:00
Code size is a little bigger than React.js, but still quite small. The todomvc example clocks in at roughly 79K gzipped, using advanced compilation.
2013-12-19 12:11:02 +00:00
## About
The idea and some of the code for making components atom-like comes from [pump](https://github.com/piranha/pump). The reactive-atom idea (and some code) comes from [reflex](https://github.com/lynaghk/reflex).
The license is MIT.