nosco / hx

A simple, easy to use library for React development in ClojureScript.
MIT License
247 stars 16 forks source link

Add displayName to defnc components #33

Closed orestis closed 5 years ago

orestis commented 5 years ago

I was using the react dev tools in Chrome and noticed that components used the munged clojurescript function name, which is not that pretty.

Turns out you can set a displayName attribute and React will use that instead. Since defnc is a macro, we have access to the original name and namespace, so therefore you can do:

(let [...
       namespace (str *ns*)]
`(do
   (defn ~name ... )
   (set! (.-displayName ~name) (str ~namespace "/" '~name)))

I'm not 100% sure yet if this will work nicely with release builds, but I'm opening an issue to see if you would be willing to accept this. If yes, I'll investigate properly and make a PR. I also wanted to look into propTypes since they use the same mechanism.

lilactown commented 5 years ago

Sure! Seems like it would be more readable that way.

We should probably use gobj/set rather than set! to avoid errors under advanced compilation.

It might increase the bundle size slightly (since the displayName won't get minified), but seems fine.

orestis commented 5 years ago

Ok, propTypes is also trivial to add. Not sure if there should be an explicit dependency on it, but my guess is that most React libraries will use it anyway, so it will be present in the bundled code anyway.

It's not 100% required, since you can provide a function to do any kind of validating needed, but it does have some nice comforts.

lilactown commented 5 years ago

Fixed the displayName in latest master. Feel free to open another issue for the propTypes thing.