mfikes / cljs-bean

Efficient JavaScript object interop via idiomatic ClojureScript
Eclipse Public License 1.0
307 stars 9 forks source link

->clj with React events #77

Open Hendekagon opened 4 years ago

Hendekagon commented 4 years ago

I've tried using ->clj with mouse events in a Reagent app with both (fn [e] (->clj e) ...) where the fn is the handler for the event, but it doesn't convert (it remains JS object). Using ->clj on the event's .-nativeEvent doesn't work either - what should I do ?

mfikes commented 4 years ago

Seems similar to #76

stevebuik commented 3 years ago

I've experienced this as well. data coming from a callback in a separate react lib built with typescript.

Fortunately I maintain the typescript lib so I was able to workaround by converting all data to plain js objects (using Object.assign) and then both ->clj and recursive bean worked fine.

Before the workaround, object? was false and then true for the plain js objects.

Not ideal for people using 3rd party libs where they can't use the same workaround at the source. However it should be possible to write an extra fn in cljs with interop until this lib can handle typescript/react js objects.

jaidetree commented 2 years ago

Ran into this myself on several occasions. Worth mentioning is that not even js->clj works on React events or native events because they are not plain objects, they are specific classes that I don't think implement the mechanisms that functions like js->clj and bean rely on to collect props into a map.

Most of the time I've used lightweight functions to transform the events to clj maps:

(defn event->map
  [e]
  {:type           (.-type e)
   :target         (.-target e)
   :current-target (.-currentTarget e)
   :prevent-default #(.preventDefault e)
   ;; ... other keys if needed
   })