Fork me on GitHub
#re-frame
<
2019-07-24
>
Ashley Smith11:07:02

As a beginner, am I going to be better off using re-frame instead of reagent? I really like building my own logic with respect to the html dom, but at the end of the day I'm not a web designer and I'm wondering whether I'd be better off switching to re-frame and learning to use that instead of reagent, instead of switching half way through?

manutter5111:07:26

I think re-frame greatly simplifies development by providing a clean structure and separation of concerns, so I’d highly recommend it. If you want the result to look nice, I’d also recommend checking into Bulma — a CSS library like Bootstrap or Foundation, but with no JS dependencies to fight with.

Ashley Smith11:07:07

I have looked at it actually

Ashley Smith11:07:52

and I'm 100% interested. So when you say re-frame provides structure, what do you mean? Do you still write things such as [:div, or does it abstract away the html components?

manutter5111:07:45

It uses the [:div syntax like reagent does (in fact it’s built on top of reagent)

manutter5111:07:26

You might be interested in a talk I did at the last conj on re-frame and building components https://www.youtube.com/watch?v=JCY_cHzklRs

👍 4
knubie12:07:50

Really enjoyed this talk!

manutter5111:07:19

it goes over how re-frame sets up the flow of events and data.

Ashley Smith11:07:59

Thank you!! If it simply enhances reagent then I don't see a problem with using it!

Ashley Smith11:07:46

In that thumbnail, it talks about app-db. My goal for today is to actually learn how to connect my front end to my backend, I was under the impression that re-frame is simply just improved reagent with structure, is that the case?

Lu12:07:01

You can use re-frame for that with this extra lib:

Ashley Smith12:07:43

I was also looking into liminus you see, Im trying to research what my options are

manutter5112:07:26

Yes, and app-db is part of that structure. The app-db is where you put all the data that your client uses. So for example, you’ll make an AJAX call to the server to get, say, a list of todo items, and put them in the app-db. Once they’re in the app-db, they’ll render automatically, because you’ll have built components that subscribe to the app-db and render whatever they find there.

Ashley Smith12:07:09

okay! so is re-frame-http-fx an alternative to luminus and thus if I use re-frame I don't need luminus?

manutter5112:07:55

Luminus is kind of a bigger picture. Let me give you some background.

Ashley Smith12:07:21

thank you, I apologise for not being so clued up

manutter5112:07:58

Some languages like Ruby have frameworks like Rails that are basically a pre-built web application with both front and back end, and you basically just customize them to get a web app up and running quickly.

manutter5112:07:45

Clojure, by contrast, doesn’t have a big all-in-one framework like Ruby does. Instead, you get each little piece by itself: a web server, and AJAX plugin, a front-end client, etc.

manutter5112:07:52

Luminus is a tool for putting together a bunch of the individual pieces so that you end up with something similar to Ruby on Rails. It’s very customizable, and you can, for instance, specify that you want to make a web app that uses re-frame.

manutter5112:07:31

re-frame-http-fx is an additional plugin that you add in addition to re-frame to make it easy to do AJAX calls with re-frame.

manutter5112:07:41

I think luminus will give you re-frame-http-fx along with re-frame, but it’s easy to add manually if luminus doesn’t give it to you automatically.

Ashley Smith12:07:21

okay so it sounds like I'll be using both. Does re-frame sit in the front end, back end or in both? I imagine luminous sits in the back?

manutter5112:07:20

Luminus is more like the cook that goes to the store, gets the ingredients, and then comes back and puts them together for you. One of the ingredients will be re-frame, which sits on the front end and manages your user interface for you. Another will be the web server (jetty, or possibly http-kit), which sits on the back end. Once your project is created, you won’t be using luminus itself, you’ll be using the skeleton application that luminus put together for you. (You’ll probably also be referring to the docs on the luminus web site a lot, he’s got some pretty good reference material there).

Ashley Smith12:07:11

okay, I think this going to be not so trivial as I thought it might be

Ashley Smith12:07:26

anyway I need to go and dofamily things now sadly, hopefully I'll crack this later!

manutter5112:07:44

:thumbsup: Good luck and feel free to post either here or in #beginners if you have any questions.

Ashley Smith13:07:35

So I've got the reframe example in my front end, but for some reason figwheel-main isn't hot reloading the page like it did with reagent. I have to manually refresh the page

Ashley Smith13:07:44

Im using this example, is it because of the event system that I have to refresh it or something?

manutter5113:07:34

Hmm, we recently switched from figwheel sidecar to figwheel main and had some trouble with hot-reloading too.

Ashley Smith13:07:09

it really is a shame as I really love figwheel with reagent, allows me to be playful

manutter5113:07:36

Try putting in a (js/console.log "Loaded page.") right before the (defn ui

Ashley Smith13:07:22

Loaded page only prints once

manutter5113:07:26

That will print a message in your browser console whenever the page re-loads, which will tell you whether it’s figwheel failing to reload your page, or React failing to re-render.

Lu13:07:29

Doesn’t figwheel require the use of one of its hooks to hot reload?

manutter5113:07:10

That could be, I’m not real expert on figwheel

Ashley Smith13:07:14

@manutter51 if I edit the page, save and switch to browser, figwheel reloads but nothing changes and the console doesn't print loaded page again

manutter5113:07:32

Ok, :thinking_face:

Ashley Smith13:07:33

I've ammended the top with this

Ashley Smith13:07:45

(ns ^:figwheel-hooks client.space.core
  (:require [reagent.core :as reagent]
             [re-frame.core :as rf]
             [clojure.string :as str]))

Ashley Smith13:07:06

so.. the hook is there, I think?

Lu13:07:18

The ^after-load one? :)

knubie13:07:28

I’ve had more success with shadow-cljs over figwheel when it comes to clojurescript

Lu13:07:19

Do this to your entry point

danielneal13:07:04

@ashley have you got the reagent example you had working

danielneal13:07:30

there might be a difference - re-frame shouldn’t stop figwheel from working, so it might just have been set up differently

Ashley Smith14:07:17

That looks like you got it! @lucio

;; -- Entry Point -------------------------------------------------------------

(defn ^:export run
  []
  (rf/dispatch-sync [:initialize])  ;; puts a value into application state
  (reagent/render [ui]              ;; mount the application's ui into '<div id="app" />'
                  (js/document.getElementById "app")))

;; This is called every time you make a code change
(defn ^:after-load reload []
  (rf/dispatch-sync [:initialize])  ;; puts a value into application state
  (reagent/render [ui]              ;; mount the application's ui into '<div id="app" />'
                  (js/document.getElementById "app")))
  ;(set! (.-innerText (js/document.getElementById "app")) "Hot Reloaded {{name}}!"))
So every time I reload I just run the same code as I did before

Lu14:07:37

Awesome 👌:skin-tone-3:👌:skin-tone-3:

Ashley Smith14:07:02

obviously time to simplify

Ashley Smith14:07:12

but I'm glad I can use reframe comfortably! ❤️

Ashley Smith14:07:35

For future reference, is ^ special or is it a naming convention for a type of function?

danielneal14:07:47

it’s a shorthand for adding metadata

danielneal14:07:43

you won’t see it that often in normal code - but it crops up often in tooling, a bit like annotations in other languages

Lu14:07:27

You could see it quite often if you loop over some data to render in your html .i.e. ^{:key “foo”} .. I find myself using this specific one a lot

Ashley Smith14:07:30

I like re-frames systems from the small snippet I've seen. Its elaborate but it does save me from having to write my own when my program gets a bit bigger

Ashley Smith14:07:36

thank you 🙂

danielneal14:07:59

yes - I find it quickly fades into the background once there’s more of your application code, and adds a nice structure to proceedings

Ashley Smith14:07:27

and I can still use my reagent knowledge seamlessly

Ashley Smith14:07:48

okay so this is all going well, now to do some reading about the backend stuff like I was asking about earlier. But I'm glad I understand events a bit better

yogidevbear17:07:44

@ashley if you're interested in two great video series on reagent and re-frame, I would highly recommend https://www.learnreagent.com/ and https://www.learnreframe.com/. Both created by @jacek.schae

❤️ 12
yogidevbear17:07:10

Very well constructed and easy to digest

Ashley Smith17:07:23

Thank you very much @yogidevbear @jacek.schae!! I'll give them a watch. Right now I'm focusing on the backend, so I won't be doing much front end until I've got the entire picture!

👍 4