Fork me on GitHub
#clojurescript
<
2021-08-10
>
Asko Nōmm02:08:56

How would I distinguish between a function defined in JS and a function defined in CLJS, from CLJS?

Asko Nōmm02:08:32

In one case, I'd like to do some clj->js transformations and in other case do nothing.

souenzzo03:08:06

maybe you can test (if (coll? x) (clj->js x) x) should be safe always to call js->clj

souenzzo03:08:49

for advanced clj->js->clj v

Brandon Stubbs02:08:56

Hey, can anyone recommend a good table library? (I am new to reagent and am not sure if the new hook approach in react-table v7 is compatible)

p-himik07:08:25

React does support hooks, it's documented.

Brandon Stubbs13:08:09

I meant using the hooks within reagent, I am not sure how to approach it. For instance react tables v7 exposes the useTable hook

Brandon Stubbs13:08:10

Yeah that is a great example on creating a hook within the component. I am battling to find an example of re-using a pluggable hook another library exposes

p-himik14:08:14

Can you link a JS example?

Brandon Stubbs14:08:29

Sure https://react-table.tanstack.com/docs/examples/basic In particular:

const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable({
    columns,
    data,
  })

p-himik14:08:05

But that's exactly what the Reagent documentation example I linked you to does..?

p-himik14:08:24

It will simply be

(let [^js table (useTable #js {:columns columns, :data data})
      get-table-props (.-getTableProps table)
      ...]
  ...)
So at the end of the day it's more of a question of CLJS-JS interop rather than what Reagent supports.

p-himik14:08:11

Of course, you don't need to bind every field from the resulting object to a separate name - you can just use them via interop as well, like (.getTableProps table) instead of (let [f (.-getTableProps table)] (f)).

Brandon Stubbs14:08:41

Ah thanks! Yeah sorry being completely new to CLJS I am not sure which are the parts that are working and which aren't still making my way through all of the courses. Thank you for being such a great help! This really was the last hurdle that was blocking me

p-himik14:08:58

Sure thing, and no need to be sorry - we all start somewhere. A few things, just in case: • There's the #beginners channel that might come in handy, especially if your question is more CLJ oriented • Don't work with multiple new things at a time - try to get comfortable with them one by one. CLJS-JS interop should be a good first thing, if you aren't comfortable with it yet.

Brandon Stubbs14:08:50

Great thanks 🙂

Ben Hammond07:08:53

(crossposted from #uix in the hope of gaining more traction...) I am experimenting with UIx (https://github.com/roman01la/uix) and MaterialUI (https://material-ui.com) and ShadowCljs I am reimplementing the Sliding Dialog from https://material-ui.com/components/dialogs/#transitions and I hit a roadblock with the

const Transition = React.forwardRef(function Transition(props, ref) {
bit Does UIx have an idiomatic way to handle react/forwardRef? Seems like because forwardRef creates a render function that takes an extra parameter for the ref and uix.core.alpha/as-element only ever passes a single props parameters then there is a mismatch I can cajole it into working if I bypass UIx thus
(defn Transition
  "Ive not found a uix idiomatic way to handle the extra parameter of forwardRef"
  []
  (r/forwardRef (fn [props ref]
                  (set! (.-ref props) ref)
                  (set! (.-direction props) "up")
                  (r/createElement Slide/default props))))
but I'm wondering if there is a more harmonious way to achieve this.... I dont completely grok how the forward Ref fits in to the bigger React/Component picture, so it seems quite likely I have missed something... Here is the gist https://gist.github.com/hammonba/c01431cbac4cc852efad408cf8dc0c90

lilactown16:08:10

you could try using a defn that takes ref as a prop

lilactown16:08:59

(defn transition
  [props]
  [:> Transition (assoc props :direction "up")])

lilactown16:08:42

the purpose for forwardRef is to paper over a historical problem in the React API

lilactown16:08:45

TL;DR when using React class components, refs could be used to get a ref to a DOM node OR they could be used to get access to the instance of a class. you typically didn't want to forward refs when you passed along props to another element in the latter case. So React removes the prop "ref" before it passes the props object to your component's render by default Since we live in an age where most components are functions now, we don't have need to get access to a class instance (since there isn't one) so it might make more sense to pass along refs, but they haven't made it the default because it might break backwards compatibility. UIx can get around this though because UIx doesn't pass props down like normal. So there's no "ref" key for React to remove when you pass in [transition {:ref ref}]

lilactown16:08:06

ah, I do see that you are not the one to render your transition component tho. so I think what you posted is actually the best way to do this. I'm not intimately familiar with how UIx as-react works with refs

👍 2
Christopher Gsell15:08:08

Non-technical question for you guys: I needed to format an integer into a string with commas and I know clojurescript does not have a format function so I made a quick function to do so. Where should I share it so that somebody can just look up on google "format integer clojurescript" and use the code? Github Gist? Might save some people, especially beginners, some time...

emccue15:08:08

The "simplest" way is to make a github repo with a basic deps.edn setup, then have a thing they can copy paste from the readme to include the git dependency

emccue15:08:41

The second simplest way is to do the same with deps or lein and have it published on jitpack

emccue15:08:11

but as far as SEO is concerned, idk

Christopher Gsell15:08:03

what does SEO mean?

dpsutton15:08:19

(.toLocaleString 1234556677534.34) results in "1,234,556,677,534.34". There might be a built-in that serves your purposes

dpsutton15:08:42

this has the added benefit of presumably working with different separators

Christopher Gsell15:08:06

wow haha ok looks like the functionality's already there. wish it was easier to locate

emccue15:08:19

"search engine optimization" - how to appear when someone googles "format integer clojurescript"

Christopher Gsell15:08:48

ahhh ok thank you

dpsutton16:08:26

i found that because i first checked to see if the goog library had something. it did not so i then just googled it

Christopher Gsell16:08:10

Ok you googled for a js function bc you knew it would be interoperable right?

dpsutton16:08:38

not sure i follow. i looked at js functions because you can always use interop with the host language

Christopher Gsell16:08:52

Gotcha gotcha thank you!

dpsutton16:08:19

ah that's what i was looking for first

lilactown16:08:45
replied to a thread:(crossposted from #uix in the hope of gaining more traction...) I am experimenting with UIx (https://github.com/roman01la/uix) and MaterialUI (https://material-ui.com) and ShadowCljs I am reimplementing the Sliding Dialog from https://material-ui.com/components/dialogs/#transitions and I hit a roadblock with the const Transition = React.forwardRef(function Transition(props, ref) { bit Does UIx have an idiomatic way to handle `react/forwardRef`? Seems like because forwardRef creates a render function that takes an *extra* parameter for the ref and `uix.core.alpha/as-element` only ever passes a *single* props parameters then there is a mismatch I can cajole it into working if I bypass UIx thus (defn Transition "Ive not found a uix idiomatic way to handle the extra parameter of forwardRef" [] (r/forwardRef (fn [props ref] (set! (.-ref props) ref) (set! (.-direction props) "up") (r/createElement Slide/default props)))) but I'm wondering if there is a more harmonious way to achieve this.... I dont completely grok how the forward Ref fits in to the bigger React/Component picture, so it seems quite likely I have missed something... Here is the gist https://gist.github.com/hammonba/c01431cbac4cc852efad408cf8dc0c90

TL;DR when using React class components, refs could be used to get a ref to a DOM node OR they could be used to get access to the instance of a class. you typically didn't want to forward refs when you passed along props to another element in the latter case. So React removes the prop "ref" before it passes the props object to your component's render by default Since we live in an age where most components are functions now, we don't have need to get access to a class instance (since there isn't one) so it might make more sense to pass along refs, but they haven't made it the default because it might break backwards compatibility. UIx can get around this though because UIx doesn't pass props down like normal. So there's no "ref" key for React to remove when you pass in [transition {:ref ref}]

Christopher Gsell18:08:09

Do y'all know why clojure.string/split functions differently in clojure than clojurescript? For example: In Clojure, (clojure.string/split "3456" #"") produces ["3" "4" "5" "6"], vs in ClojureScript it produces ["" "3" "4" "5" "6"]

dpsutton18:08:49

regexes in Clojure and ClojureScript use the host implementations. Those will have differences

dpsutton18:08:36

has an entry for regexes

🙏 6