clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
sova-soars-the-sora 2021-07-01T03:02:21.114300Z

Maybe try without the /revision/latest/top-crop stuff after .jpeg

Tomaz Bracic 2021-07-01T08:16:49.114700Z

hi

πŸ‘‹ 4
Simon 2021-07-01T10:55:25.117200Z

What is the best way to fetch something from an API and display it in a reagent component? I found this approach where they use ajax to reset the state of a reagent atom. But is there a better way?

(ns breaking-bad-quotes.core
  (:require [reagent.core :as r :refer [atom]]
            [ajax.core :refer [GET]]))

(defn fetch-link! [data]
  (GET "<https://breaking-bad-quotes.herokuapp.com/v1/quotes>"
    {:handler #(reset! data %)
     :error-handler (fn [{:keys [status status-text]}]
                      (js/console.log status status-text))}))

(defn quote []
  (let [data (atom nil)]
    (fetch-link! data)
    (fn []
      [:div.cards&gt;div.card
       [:h2.card-header.text-center "Breaking Bad Quotes"]
       [:div.card-body.text-center
        [:p#quote @data]
        [:p#author @data]]
       [:div.card-footer.center.text-center
        [:button#twitter.outline&gt;a#tweet
         {:href "#"
          :target "_blank"}
         [:i.fi-social-twitter " Tweet"]]
        [:button#new-quote.outline
         [:i.fi-shuffle " New Quote"]]]])))
https://www.rockyourcode.com/tutorial-clojurescript-app-with-reagent-for-beginners-part-1/

p-himik 2021-07-01T10:57:45.117400Z

"Better" in what sense? Impossible to give any advice without knowing exactly what you want to achieve.

Simon 2021-07-01T11:06:42.117600Z

You are right @p-himik. I was mainly just thinking that coming from React.js and JS it is such a standard practice to make a component with a useEffect hook where fetch is called, which uses setState to update a value which then gets displayed. Is there a similarly standard approach for this in CLJS with Reagent? I have looked into cljs-http, core.async and even js/fetch . But I'm not quite sure about which one to use in this scenario: I simply need to fetch something from and external HTTP API and display it in a reagent component.

p-himik 2021-07-01T11:13:43.117900Z

Even in React, using a hook is a practice, not something standard.

p-himik 2021-07-01T11:14:04.118100Z

You can use hooks with Reagent just fine. Or you can use atoms. Or you can use re-frame. Or maybe something else.

p-himik 2021-07-01T11:14:37.118300Z

Generally, people don't use hooks with Reagent simply because Reagent atoms are simpler and sufficient.

p-himik 2021-07-01T11:15:31.118500Z

> I simply need to fetch something from and external HTTP API and display it in a reagent component. If you care about nothing else, then your current code LGTM.

Simon 2021-07-01T11:18:57.118700Z

Thank you for your response. i'm still quite new in the Clojure world. When would it make sense to make use of re-frame?

p-himik 2021-07-01T11:21:01.118900Z

When you have a decent amount of state that you have to manage.

Simon 2021-07-01T11:22:13.119200Z

Yes that was also my understanding πŸ™‚ Thanks again for responding so quickly and well @p-himik

πŸ‘ 1
2021-07-01T11:41:09.122300Z

Hey everyone, I am quite curious to hear from those who built applications with Elm before, what benefits does Clojurescript bring to the dev experience over Elm? Sadly, Elm React-Native never took off so I'm looking into Cljs RN. Might as well use Cljs for the SPA while I'm at it.

nate sire 2021-07-02T18:15:26.159200Z

Clojure is incredibly stable and requires very little maintenance... but Node seems to be always breaking things.

nate sire 2021-07-02T18:16:29.159400Z

Also... Node added a really weird way of supporting 2015 ecma script modules... where you need to copy all your files to an .mjs extension.... but I do like Node because it's rapid when building things.

nate sire 2021-07-02T18:16:59.159600Z

Clojure takes more thought but over the long term... much more stable... it's a trade off

2021-07-01T11:42:23.122600Z

interop with javascript is at least a lot easier

2021-07-01T11:43:22.122800Z

also, the dev-experience ones you get used to a REPL, hot-reloading etc

2021-07-01T11:46:17.123Z

I’m almost done with an app using Krell and I had to interop quite a bit with react libraries, so I think it would be awefull with Elm to be honest

Simon 2021-07-01T12:39:58.125300Z

#beginner is there a similar option to the || fall back from javascript. For example as used in this context:

&lt;img src={image.might.be.nil || "default.jpg"} /&gt;

p-himik 2021-07-01T12:40:52.125400Z

(or value1 value2)

p-himik 2021-07-01T12:41:35.125700Z

Note that in both cases the second value will be used if the first one is false.

Simon 2021-07-01T12:41:36.125900Z

oh yeah that actually works

Simon 2021-07-01T12:42:03.126100Z

probably it compiles to the same expression

Simon 2021-07-01T12:42:15.126300Z

thanks

rakyi 2021-07-01T13:33:31.126500Z

here’s one experience report https://www.youtube.com/watch?v=geeK1-jjlhY

nate sire 2021-07-01T14:16:57.126900Z

For me... the ability to use Clojure Script interop with Node/TypeScript because of WASM

Tomaz Bracic 2021-07-01T19:03:38.130800Z

sorry for my really beginners questions. i am a bit confused about Reagent and Re-frame. What exactly does this "relation" to React ecosystem means? Is the "output" of my clojurescript project that is based on Reagent ...like editable React "component" that someone can use as part of react codebase in a project or what?

Tomaz Bracic 2021-07-01T19:04:07.131300Z

or is output pure javascript code?

Tomaz Bracic 2021-07-01T19:04:17.131600Z

like vanila js

p-himik 2021-07-01T19:05:23.131700Z

Re-frame uses Reagent. Reagent uses React. Regardless of what you use, in the end you will have a JS file or files that browsers can run.

emccue 2021-07-01T19:10:53.131900Z

A react component is usually a function that returns a "react element"

Tomaz Bracic 2021-07-01T19:10:53.132100Z

but what about other great Elm benefits...like refactoring and maintenance of a codebase, when you can literaly dig into some specific part that you still remember about ...and then for the rest ... compiler directs you. Don't get me wrong... i just recently "moved" here... but for reasons around how Elm core team drives development of Elm... where you have a feeling that Elm is just a private project... shared with public... and that rarely community is heard

emccue 2021-07-01T19:11:05.132300Z

which is an object you get from calling React.createElement(...)

Tomaz Bracic 2021-07-01T19:11:36.132500Z

But i am still not fully sure, that i did the right decision. I still try to persuade myself i did.... every day

emccue 2021-07-01T19:11:36.132700Z

A reagent component is usually a function that returns "hiccup" data, which is a format that can be converted to a react element

Tomaz Bracic 2021-07-01T19:11:48.132900Z

so i am interested in others opinion

Tomaz Bracic 2021-07-01T19:14:17.133100Z

aha... so Reagent uses React functionalities... which i need to be aware of then. :) and the output of a project is vanila JS. ok, thanks!

nate sire 2021-07-01T19:19:35.133400Z

I think the role you use a language (contractor, enterprise employee, cofounder) will impact your reasoning to keep commited to Clojure

Tomaz Bracic 2021-07-01T19:21:03.133600Z

what do you mean with that exactly?

nate sire 2021-07-01T19:21:43.133800Z

for example.... I think many of us want to use a popular language because we think that helps us find a job

nate sire 2021-07-01T19:22:29.134Z

even though Clojure is simple... it can take time to learn... so demand can be high in the market and there is a barrier to entry

nate sire 2021-07-01T19:22:49.134200Z

contrast that to JS... low barrier... bigger market

nate sire 2021-07-01T19:23:00.134400Z

but more competition

Tomaz Bracic 2021-07-01T19:23:40.134600Z

aha...

nate sire 2021-07-01T19:24:44.134800Z

that's why I use JS, Typescript, Clojure and CLJS... it gives me both strategies

nate sire 2021-07-01T19:25:36.135Z

big fish in small pond (I am still a small fish)... or small fish in big pond

nate sire 2021-07-01T19:26:17.135200Z

other than that... ignoring any market reason... just pick what you like to work with

Tomaz Bracic 2021-07-01T19:32:52.135400Z

would you say (since you use both worlds) ... that for instance.... you need to equaly frequently devote time for maintaining the past projects... (updating, upgrading packages...) or would you say that Clojurescript allows you to do that less frequently. This is one really big plus for Elm ... you might be aware of, for some, slow development... but for some... this is a huge plus.

Tomaz Bracic 2021-07-01T19:33:34.135600Z

or your two ponds require equally amount of effort for this? :)