All Projects → reagent-project → Reagent

reagent-project / Reagent

Licence: mit
The idea and some of the code for making components atom-like comes from pump. The reactive-atom idea (and some code) comes from reflex.

Programming Languages

clojure
4091 projects
CSS
56736 projects
shell
77523 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to Reagent

Tincture
Frontend development toolkit for ClojureScript
Stars: ✭ 24 (-99.45%)
Mutual labels:  reagent, frontend
Antizer
ClojureScript library for Ant Design React UI components
Stars: ✭ 234 (-94.61%)
Mutual labels:  reagent, frontend
tailwind-hiccup
tailwindcss + hiccup = 👍👍
Stars: ✭ 34 (-99.22%)
Mutual labels:  reagent, hiccup
Beam
✨ Expressive WebGL
Stars: ✭ 383 (-91.18%)
Mutual labels:  frontend
Create React App Typescript
DEPRECATED: Create React apps using typescript with no build configuration.
Stars: ✭ 3,759 (-13.47%)
Mutual labels:  frontend
Chart Race React
📊 Seamless bar chart race component for React.
Stars: ✭ 406 (-90.65%)
Mutual labels:  frontend
Mobx Angular
MobX connector to Angular
Stars: ✭ 432 (-90.06%)
Mutual labels:  frontend
Aprenda Frontend
📚 Central de conhecimento sobre front-end
Stars: ✭ 381 (-91.23%)
Mutual labels:  frontend
Conduit
Real world application built with ClojureScript + re-frame
Stars: ✭ 422 (-90.29%)
Mutual labels:  reagent
Front End Design Checklist
💎 The Design Checklist for Creative Web Designers and Patient Front-End Developers
Stars: ✭ 4,136 (-4.79%)
Mutual labels:  frontend
Front End Handbook 2019
[Book] 2019 edition of our front-end development handbook
Stars: ✭ 3,964 (-8.75%)
Mutual labels:  frontend
Iceworks
Visual Intelligent Development Pack(可视化智能开发套件)
Stars: ✭ 390 (-91.02%)
Mutual labels:  frontend
Vue Skeleton Mvp
VueJs, Vuetify, Vue Router and Vuex skeleton MVP written on JavaScript using async/await built to work with API REST skeleton: https://github.com/davellanedam/node-express-mongodb-jwt-rest-api-skeleton
Stars: ✭ 406 (-90.65%)
Mutual labels:  frontend
Pingy Cli
The Simple Frontend Build Tool. No Configuration, No Plugins.
Stars: ✭ 390 (-91.02%)
Mutual labels:  frontend
Annotated Webpack Config
This is the companion github repo for the "An Annotated webpack 4 Config for Frontend Web Development" article.
Stars: ✭ 425 (-90.22%)
Mutual labels:  frontend
Learning Roadmap
The Front-End Developer Learning Roadmap by Frontend Masters
Stars: ✭ 336 (-92.27%)
Mutual labels:  frontend
Client
The Hypothesis web-based annotation client.
Stars: ✭ 416 (-90.42%)
Mutual labels:  frontend
Frontend Playbook
The Frontend Playbook
Stars: ✭ 395 (-90.91%)
Mutual labels:  frontend
Re Frisk
Take full control of re-frame app
Stars: ✭ 396 (-90.88%)
Mutual labels:  reagent
Udash Core
Scala framework for building beautiful and maintainable web applications.
Stars: ✭ 405 (-90.68%)
Mutual labels:  frontend

Reagent

Run tests Clojars Project codecov cljdoc badge project chat

A simple ClojureScript interface to React.

Reagent provides a way to write efficient React components using (almost) nothing but plain ClojureScript functions.

Usage

To create a new Reagent project using Leiningen template simply run:

lein new reagent myproject

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.

To use Reagent in an existing project you add this to your dependencies in project.clj:

Clojars Project

And provide React using either npm (when using e.g. Shadow-cljs)

npm i react react-dom

or by adding Cljsjs React packages to your project:

[cljsjs/react "17.0.2-0"]
[cljsjs/react-dom "17.0.2-0"]

Note: Reagent is tested against React 17, but should be compatible with other versions.

Examples

Reagent uses Hiccup-like markup instead of React's sort-of html. It looks like this:

(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.

[:div
  [:p
    [:b "Nested Element"]]]

can be written as:

[:div>p>b "Nested Element"]

Since version 0.8: The :class attribute also supports collections of classes, and nil values are removed:

[:div {:class ["a-class" (when active? "active") "b-class"]}]

You can use one component inside another:

(defn calling-component []
  [:div "Parent component"
   [some-component]])

And pass properties from one component to another:

(defn child [name]
  [:p "Hi, I am " name])

(defn childcaller []
  [child "Foo Bar"])

You mount the component into the DOM like this:

(defn mountit []
  (rd/render [childcaller]
            (.-body js/document)))

assuming we have imported Reagent like this:

(ns example
  (:require [reagent.core :as r]
            [reagent.dom :as rd]))

State is handled using Reagent's version of atom, like this:

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

(defn state-ful-with-atom []
  [:div {:on-click #(swap! click-count inc)}
   "I have been clicked " @click-count " times."])

Any component that dereferences a reagent.core/atom will be automatically re-rendered.

If you want to 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:

(defn timer-component []
  (let [seconds-elapsed (r/atom 0)]
    (fn []
      (js/setTimeout #(swap! seconds-elapsed inc) 1000)
      [:div
       "Seconds Elapsed: " @seconds-elapsed])))

This way you can avoid using React's lifecycle callbacks like getInitialState and componentWillMount most of the time.

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:

(defonce my-html (r/atom ""))

(defn plain-component []
  [:p "My html is " @my-html])

(def component-with-callback
  (with-meta plain-component
    {:component-did-mount
     (fn [this]
       (reset! my-html (.-innerHTML (rd/dom-node this))))}))

See the examples directory for more examples.

Performance

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).

The ClojureScript overhead is kept down, thanks to lots of caching.

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.

About

The idea and some of the code for making components atom-like comes from pump. The reactive-atom idea (and some code) comes from reflex.

The license is MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].