Fork me on GitHub
#helix
<
2020-05-05
>
lilactown00:05:32

ah I found some old code I wrote where I had made this hook:

(defn useChan [chan on-take on-close]
  (hooks/use-effect
   [chan on-take on-close]
   (let [cleanup? (a/chan)]
     (a/go-loop []
       (let [[v ch] (a/alts! [chan cleanup?])]
         (cond
           (= ch cleanup?) (on-close)
           (nil? v) (on-close)
           :else (do (on-take v)
                     (recur)))))
     #(do (a/put! cleanup? true))))))

👀 1
James Amberger03:11:24

Why do we need the 2nd element of the use-effect to be [chan on-take on-close]?

lilactown17:11:44

if the chan on-take or on-close values change, we want to restart the go-loop with the new values. that vector is of any dependencies that should cause the effect to re-run

1
Aron00:05:42

thanks, that's very nice

Aron00:05:13

not a defhook macro at least : D I am guessing that's why the go-loop recur is complaining

lilactown02:05:29

does go-loop and recur not work when used inside defhook?

Aron02:05:54

I am probably doing something wrong, it's a bit late at night, I need a coffee : )

Aron02:05:25

I was getting a lot of help from people, and tried out always the easiest. Will check the useChan right now

Aron02:05:38

in #beginners

Aron02:05:38

and from all the responses together I get the feeling that the problem is somewhere else

Luis C. Arbildo05:05:06

Howdy, How can I do if I want add on-click event to style-components

(defstyled StyleNumberButton
  :div
  {:display "flex"
   :color "white"
   :border "1px solid black"
   :border-radius "10px"
   :background "gray"
   :height "100px"
   :width "50px"})

lilactown19:05:56

I’m not familiar with styled-components, sorry

lilactown19:05:17

I would assume you can pass in an on-click handler to it when you invoke it?

Luis C. Arbildo19:05:50

No ... you just can pass an component

lilactown19:05:28

I don’t know what you mean

lilactown19:05:43

I’m guessing you can do (StyleNumberButton {:on-click ,,,}) or something?

lilactown19:05:58

again, I do not know how this CLJS styled-components library works

Luis C. Arbildo19:05:26

I guessed that too, but that doesn't work ... I was wondering if is possible helix give this returned element the possibility to add this events

lilactown19:05:56

I don’t see how helix comes into this?

lilactown19:05:50

if that doesn’t work, it’s a bug with styled components and you should file a report

👍 4
lilactown19:05:54

you could try :onClick

👍 4
💯 4
Luis C. Arbildo19:05:55

I will try, thank you

Luis C. Arbildo19:05:45

WORKS WITH :onClick !!! thank you master

Aron08:05:45

@lilactown definitely something else, because not even this example works https://clojurians.slack.com/archives/C053AK3F9/p1588644858480300 basically, any time I used recur it said it cannot do it

Aron09:05:09

but (go (loop instead of go-loop seems to work. Dunno why I haven't tried this first?

Aron16:05:07

I am not sure the ($ macro is helping me with the props thing

lilactown16:05:18

happy to help if you explain the problem 🙂

lilactown17:05:11

if you’re ever curious about what $ is doing you can use macroexpand:

(macroexpand '($ foo {:bar "baz"}))
;; => (. (helix.core/get-react) createElement foo (helix.impl.props/props {:bar "baz"}))

lilactown17:05:43

if you macroexpand the helix.impl.props/props call you can see exactly how it creates the JS object:

(macroexpand '(helix.impl.props/props {:bar "baz"}))
;; => (let* [obj85058 (js* "({~{}:~{}})" "bar" "baz")] obj85058)

Aron17:05:13

@lilactown (mapv #($ ListFile {& %}) files) ? 🙂

Aron17:05:06

(apply <> (mapv I think, but inside the mapv?

Aron17:05:31

well, it works without the apply I think, haven't read the source

lilactown18:05:46

yeah you should be able to pass a seq of elements to any other element, but you probably need to add a :key prop to help React

Aron20:05:36

@lilactown but I can't create the seq of elements like that

Aron20:05:09

is there maybe a branch or something with an easy sandbox to demonstrate

Aron20:05:09

Ok, I think I got it working. In retrospect, it's probably me being very inept with clojure what's causing most of my problems.

tomc20:05:57

If I want to pass a non-literal map to a component as props, is using a factory the best option? Eg ((helix/factory my-component) my-map) ?

tomc20:05:48

Thanks. So if I have my-map as a local, do I need something like ($ component {& my-map}) ?

tomc21:05:15

Thanks again. Maybe the docs should mention this variant of dynamic props. It wasn't immediately obvious that this was the preferred solution, though it makes sense now that I know it.

lilactown21:05:59

Do you mean that the docs I linked are not clear that you can use a local binding?

tomc21:05:16

That part was clear. What wasn't clear to me was that this is the preferred way to pass props to a subcomponent when all you have is a local binding, since we also have the option to use factories, and the factory approach seems to produce the same result.

lilactown21:05:26

factories vs $ is sort of a separate issue. they both solve the same problem, it’s a question of syntax

lilactown21:05:45

I’ve written almost no docs on factory functions

lilactown21:05:02

we don’t use them on our project at work

lilactown21:05:22

I would accept a PR to clarify the docs, I’m just not sure how to be clearer yet 😄

tomc21:05:51

I think I know a decent addition, I'll get you the pr in a few minutes. thanks for the help.

tomc21:05:47

@lilactown If you're open to it, I'd be happy to also write a quick "integration with reagent" guide to help people with my use case - a primarily reagent+re-frame project with bits of helix added to make integration with js react libs easier

lilactown22:05:41

that would be awesome and super useful to people I bet!