11 ms·
Re-Frame: Build web apps in ClojureScript and React
- hellofunk 6y agoRe-frame is a layer on top of reagent, which is an API for using React in cljs. If you want to use react, you can also just directly use reagent which is the preference of many in the community. The boilerplate and verbosity required for reframe could be overkill for many small projects, especially for solo developers, where pure reagent often thrives. However reframe’s restrictions can be helpful in team environments. The library github page is an entertaining display of equal parts vanity and hyperbole: https://github.com/day8/re-frame/ https://github.com/day8/re-frame/
- synthc 6y agoRe-frame really doesn't add much in addition to Reagent, I really don't understand its popularity.
- MikeOfAu 6y agoThis comes up often enough that there is an FAQ entry: https://day8.github.io/re-frame/FAQs/DoINeedReFrame/ https://day8.github.io/re-frame/FAQs/DoINeedReFrame/
- hellofunk 6y agoI personally prefer reagent directly over re-frame. I've heard some Clojure developers express frustrations with the typical idioms that Re-frame promotes. I can see some value in how it organizes some things, but you can do the same thing directly with Reagent far more quickly. The difference is that in Re-frame you only really have one way of doing things, and that consistency can be good for a team. But it does require extra development time.
- yomly 6y agoI think the parent is correct to point out two things. That re-frame is useful in bigger teams and it is useful for larger apps. Re-frame is somewhat more complex than inline and vanilla reagent. I suppose with hooks a lot of the simpler use cases for state management also go away. That said, as a discipline the indirection re-frame encourages you to use helps you architect a purely functional* application. My observation is a lot of larger user facing applications will tend towards something that is event-driven and possibly loosely a state machine. Which incidentally also is one of the implementations of "functional core imperative shell" Ultimately YMMV. If you just want a static site with a tiny bit of sparkle then you probably don't need reframe. (possibly you don't need cljs/reagent) If you're tending towards a large full blown SPA with lots of interactivity and IO plus failure states, then reframe probably isn't a bad solution IMO. * subscribe and dispatch are strictly not pure and actually use global state so purely functional isn't actually accurate. But it is a reasonable stab at being purely functional, while strongly quarantining the side-effecting bits.
- pinchhit 6y agoI've run a team with re-frame and a team with reagent (and a convention of a single state atom.) Reagent by itself scaled far better. I use re-frame if it's a company convention, but I'd far rather just ditch it. Plain reagent has no unnecessary function registry; mutations are done with `(swap! my-cursor foo/update-foo bar)` rather than the extra overhead of `(dispatch [::foo/update-foo bar])`. This also helps new people who rely on cursive to jump to definitions, and don't have a preferred emacs setup. Reagent testing can be scoped to a single DB. In re-frame, you can clear the subscription queue (which will not handle callbacks from network calls putting events on to the queue when the callback fires, leading to flaky tests that receive unexpected events.) In reagent, you have more power, since you can call `(reagent/atom {})` with your test state and any callbacks specfic to the test will see their DB, regardless of when they resolve. Re-frame's single events/subscriptions files scaled horribly; once we got beyond a few hundred business-logic-filled events, velocity on changes involving that file slowed down a lot. We broke convention and moved events into namespaces closer to the code.
- pinchhit 6y agoThree things that would make re-frame better, if you want them: - A supported/blessed way to switch out the entire app-db entirely for the purposes of testing - Use of symbols rather than namespaced keywords for function dispatch - Docs that give alternate strategies for code organization (fine to have the events.cljs convention, but would be nice to see some viewpoints like having one per child ns similar to angular's code organization model.)
- MikeOfAu 6y agoAnswers to your three points: - I'm wondering if re-frame-test might help? - That ship has sailed a long time ago. And, even if it hadn't, I diagree that symbols would be better. - I'm unfamiliar with angular's code. The Resources section lists larger apps which you can inspect.
- pinchhit 6y agoI'll try re-frame-test, thanks! If it doesn't do something like `(with-redefs [re-frame.db/app-db (reagent/atom {})] ...)` it is still open to this bug, though. Because the call to `dispatch` happens inside of a callback the test library has no way to signal that future dispatches coming from a different function context should not be put on the same queue. It'd be hard to change that aspect of the API now, and I wouldn't recommend you do it; but right now, editor completion and new-user understanding is just better on the reagent side. They do this: (defn assoc-foo [db bar] (assoc db :foo bar)) (defn fetch-new-foo [db bar] (go (let [bar (<! (get-bar)] (swap! db assoc-foo bar)))) (swap! db assoc-foo bar) (fetch-new-foo db bar) whereas re-frame would make you do this: (re-frame/reg-event-db ::assoc-foo (fn [db [_ bar]] (assoc db :foo bar))) (re-frame/reg-fx ::pull-from-channel (fn [{:keys [f event]}] (go (let [resp (<! (f))] (re-frame/dispatch (conj event resp)))))) (re-frame/reg-event-fx ::fetch-new-foo (fn [_ _] {:pull-from-channel {:f get-bar :event [::assoc-foo]}})) (dispatch [::assoc-foo bar]) (dispatch [::fetch-new-foo]) The first one has a lot going for it; swap! is part of the standard library, has a bunch of docs for free, etc.; any clojure editor will pick up on the use of assoc-foo as a function and give you arity warnings if you pass too many parameters, etc. I've looked at the larger apps - the way I've seen people be most successful if if they have a child namespace with events, subs, & views laid out on a per namespace basis (`my-ns.events, my-ns.subs, my-other-ns.events, my-other-ns.subs`.) Partially due to the tone the docs take, there's always some pushback as to whether that's the re-frame way to do things, and I'd love to just be able to avoid that whole conversation in the future.
- synthc 6y agoI really dislike the documentation of re-frame. The concepts are quite simple, but the docs explain them in a really confusing and buzzword heavy way.
- MikeOfAu 6y agoHmm. In my experience, this an unusual opinion (to me, the author). Over the years the re-frame documentation has been the most praised part of the project. Could you point out the area which you found unnecessarily difficult, and I'll happily review it. Could you also include your background, so I know where you are coming from? The docs are a little oriented towards JS developers coming across, but perhaps you are already very experienced with Clojure and data-oriented design, which makes it seem like the concepts more obvious. Maybe.
- thom 6y agoSaid with love, happy re-frame user: many developers want to be able to match use cases to docs and code examples as quickly and directly as possible. The re-frame docs contain a lot of very verbose motivation and philosophy (over multiple pages) which while useful context, obfuscates the practical content. All of re-frame’s philosophy would be immediately evident from some code examples on the top level readme on GitHub.
- spapas82 6y agoThis is on point. Whenever I have tried reading the re-frame docs (I actually have tried more than once during the prevt years) I always get frustrated/bored from the lack of actual use cases and explanations of why this could be useful to me! The docs just seem to academical for me... They would be excellent for a master's thesis but not for somebody deciding if he would like to invest time into a project.
- MikeOfAu 6y agoThanks for the feedback. In this regard, I think things have improved a little over the last 8 months, stepwise. In our busy world, people want more immediate summaries and progress. These docs arose out of me trying to teach my team how to think about coding in Clojure and to use re-frame (we were all new to the language at the time), so they definitely have a didactic flavour. I continue to nudge them in the direction you recommend.
- dennisy 6y agoI would love to try out all these cool alternatives to pure JS plus Vue or React, but at what point is this a good idea for a commercial production application?
- invalidOrTaken 6y agoAt this point I think it's safe to say---clojurescript has proven itself and is not a risk. An excellent language, good libraries (Metosin's ambidextrous router, Reitit, stands out here), the best development story with Figwheel, and oh, dead code elimination for free, as the cljs compiler leverages Google's Closure (not to be mistaken for Clojure) compiler. Not to mention that cljs is actually a better fit for React even than JavaScript---implicit return, bias towards functional programming, and it requires no magic JSX step---in cljs you just write your html in nested data literals. It's a dream. I haven't even mentioned black magic like core.async, or that now your front-end devs have a foothold on the JVM rather than the more-impoverished Node ecosystem(oh, you want to keep Node? Well, cljs runs there too!). Pitch is a well-funded German startup making a presentation tool in the browser---as in, they can't afford to get their front end wrong---and they're comfortably using cljs. Frankly at this point I think anyone serious about the frontend, who wants it to be good, not just passable, should be using clojurescript. There are reasons not to---if you need something today, and don't have time to learn a lisp---but for anything not due next week, I'd recommend cljs.
- yakshaving_jgt 6y agoConversely, I found the experience pretty terrible when I worked on a big cljs/reagent/reframe/figwheel app over the course of a couple of years. It was slow, prone to runtime errors (though not as much as JavaScript), and often presented weird edge-cases at the intersection between the Lisp world and the React world. The company ended up abandoning all this stuff after having been big Clojure/ClojureScript evangelists for a long time. On the other hand, after having built several large Elm applications, Elm is still the tool I'd recommend. On my current ~10KLOC Elm project, compilation is near enough instant, the structure of the code is more rigid so it's far easier to understand — even for beginners — and there's far less potential for runtime errors to occur. Of course, this is the classic religious war in programming, so I expect someone to appear soon and point out that there is no empirical evidence to support the idea that a type system like the one in Elm or Haskell results in fewer errors in practice. Or perhaps they'll say jet fuel can't melt steel beams.
- b4ke 6y agothe documentation seems thirsty for validity, if i am being honest about it's attempts to impart understanding with it's quip context.
- yogthos 6y agoRe-frame is absolutely fantastic in my experience. Here's a beginner workshop on Reagent/re-frame that I ran for Js devs a little while back. If you're interested to try ClojureScript, this will run your through the basics from ground up https://github.com/ClojureTO/JS-Workshop/tree/shadow-cljs https://github.com/ClojureTO/JS-Workshop/tree/shadow-cljs
- frompdx 6y agoI enjoy working with re-frame and can't imagine building single page apps in ClojureScript without it. For some reason the docs are controversial, which is a surprise to me. I appreciate the conceptual framing in the docs and how each section builds upon the previous. I don't mind the references to star trek or statements on the greatness of Lisp. A common complaint seems to be the docs are too verbose and people just want to jump in and use the library. If this is you, I recommend using the Luminus framework https://luminusweb.com/ https://luminusweb.com/ to generate a new project template so you can hack on a ClojureScript project with re-frame to see if you like it. # Make a new project with re-frame lein new luminus my-project +re-frame # Start the clj and cljs repls lein repl lein figwheel
- filoeleven 6y agoI’ve looked at re-frame for a couple of years now, since right when I started learning Clojure on my own time, and I’m quite interested but still on the fence about whether it would be helpful for my use case. Our API calls are messy (historical limitations) and clojurescript’s robust data merging capabilities would work wonders here. In a new front-end iteration I’m trying to use the model of a single global data store, but given how many conditional API calls we have to make and how much easier and more idiomatic it is to “cheat” with mutability in JS, it’s hard to convince my team to be rigorous about keeping to this pattern for everything. There are real difficulties and tradeoffs there: ES6 spreads more than one level deep are hard to read, we may want to display a view before its drop down entries are resolved, etc. The essentials are that we have to handle retrieving the following: 1. A model that represents some global concerns (auth, network/system errors, websocket messages) plus the data required by the current view. 2. A template that determines how to display the current view. This can be a full-page display with nav tree links to other views plus the display form that a given field should use (input, drop down, button, etc.), or it can be a modal view, n layers deep, and also may or may not contain a nested nav tree of its own. (Yep, we have to keep the lower-layer models ourselves and still merge errors/websocket stuff.) I’m already confident that cljs can turn the messy model reconciliation into something easier to read and maintain. I’m less certain about re-frame’s idea of how to subscribe to its db data, especially since I’d like to reuse some custom components in deeply-nested data. For example, “sticking a drop down inside of a grid column“ is an expected feature in the view template. Transforming it from js react to cljs reagent seems like a pretty straightforward exercise—famous last words. The impression I get from re-frame is that it’s “a more robust redux with less boilerplate,” and the dev tools look great. But any proof-of-concept app I develop will be a spare-time if not my-own-time exploration. It’s something I want to do, because I think it’ll save us loads of time later and just be more fun to work with. It’s also an uphill battle to sell using Clojure at my org anyway, and I want to make a good pitch of it without spending too much time in the weeds. Does anyone have experience with a system like this in re-frame, or in cljs more generally, who is willing to give me some feedback?