Fork me on GitHub
#reagent
<
2018-06-03
>
mikethompson00:06:08

@drewverlee it is because React embodies this formula UI = f(s)

mikethompson00:06:36

The UI rendered (for the user to look at) is a function f of the applications state s. So that is the sense in which React is "functional" (and liked because of it).

đź‘Ť 4
mikethompson00:06:55

In order to be efficient about it, React does all the virtual DOM dance, but that's just implementation detail.

theeternalpulse00:06:13

with reagent, how would I fetch a key meta that's on an object that gets re-rendered. I have components that get rendered by a for loop dynamically and want to add a random key only if the original component being re-instantiated (not re-rendered) uses the same key.

theeternalpulse00:06:46

(for [c content]
       (do
         (js/console.log (meta c))
         (with-meta c {:key (js-uuid)})))

theeternalpulse00:06:16

the offending code

theeternalpulse00:06:53

since this helper is part of a for loop and content is just a rest args, it unfortunately warns about keys

mikethompson00:06:21

@theeternalpulse just to be clear: you are using a random number as a :key to force rerenders?

mikethompson00:06:02

Can you state more generally what you are trying to achieve?

mikethompson00:06:28

Have a glance at this ... just to make sure we're on the same page https://stackoverflow.com/a/37186230/5215391

theeternalpulse00:06:28

so the function is called every time I update the state, and I want only keys the first time any item is created, when this function is called again I want to check the current key and just re-use that

theeternalpulse00:06:20

Is it general practice to put a key on anything that is bound to a value that might change?

mikethompson00:06:39

Did you have a look at the link provided above?

theeternalpulse00:06:42

I guess there's a gap in understanding why it complains about keys for a for loop, but not if I just put them on the page

mikethompson00:06:49

I feel like it will help

justinlee01:06:42

for what it is worth, although mike explained it nicely, this is the key bit and it is a very sneaky thing to fully grok The list provided by the map is folded into the [:ul] vector. At the end of it, no list in sight. Just nested vectors.

justinlee01:06:20

there is a big difference in reagent between [:div '([:div] [:div])] and [:div [:div] [:div])]

justinlee01:06:29

the first one requires keys and the second one doesn’t

justinlee01:06:49

and when you use something like map, you get the first structure

justinlee01:06:17

what mike is suggesting is: if you really aren’t switching the order of the children, just use into instead of map so that you get the second structure

theeternalpulse01:06:27

ah yes, sorry I missed the answer earlier, I got it. In my case

(defn styled-component [tag styles]
  {:style/indent 1}
  (fn [attrs & content]
    [tag (merge-with str attrs {:class (apply with-styles styles)})
     (for [c content] c)])) ;;<---
becamefd
(defn styled-component [tag styles]
  {:style/indent 1}
  (fn [attrs & content]
    (into [tag (merge-with str attrs {:class (apply with-styles styles)})] ;;<---No more warnings :)
          (for [c content] c))))

justinlee01:06:20

can’t you just replace (for [c content] c) with content

justinlee01:06:58

(also, note that if you really are rendering a long list of things and you are going to be adding/subtracting/reordering items, you should think about solving this problem by assigning a random key to the original data when you create it or fetch it from the server and then using that data as the key. trying to do it on the fly is going to be a nightmare)

theeternalpulse01:06:41

yeah, I think I learned that lesson 🙂. and you're right, I forget that it's already a list

theeternalpulse01:06:32

also why do you js event listeners go haywire, I only have one set-interval event added to a level 2 component at the beginning

theeternalpulse01:06:48

it goes all the way to 700+ in the chrome tools