Fork me on GitHub
#clojurescript
<
2016-07-31
>
akiroz04:07:17

Hi! I was wodering if it's possible to do destructing on JS objects, my naive approch didn't work:

(let [{x :x} {:x 1}] x)
; => 1
(let [{x :x} #js{:x 1}] x)
; => nil

odie04:07:48

@pesterhazy: Thanks for the tip. I guess I’ll try managing stuff through npm manually then. 😃

negcx05:07:49

@akiroz: I think you need to js->clj first.

akiroz05:07:10

hmmm... alright thanks~ would be nice if I could do destructing in JS callback functions and save a line, but I guess not. the ES2015 folks just loves doing this in callbacks: ({someKey}) => ...

negcx06:07:30

Yes, I'm pretty new to ClojureScript, and coming from TypeScript / ES6 the new flavors of JS support destructuring JS/JSON objects

negcx06:07:53

not sure which callback functions you refer to but I guess I wonder why you wouldn't be able to use the CLJ data structures instead

negcx06:07:01

if it's a JS object and you only need 1key you can do something like this for an anon function, #(some-func (.-someKey %))

negcx06:07:07

e.g. a callback i use in a component for on-change is #(reset! value (-> % .-target .-value))

akiroz07:07:12

It's an event handler from a 3rd party JS library and they are passing me some JS data structures. my function is a bit long (ajax calls) so I didn't use the function literal

negcx08:07:02

@akiroz I think you want something like this then `(def data #js {"myProp" "a value" "anotherProp" "a second value"}) (let [{:strs [myProp anotherProp]} (js->clj data)] ....)`

akiroz09:07:59

that works, thanks! 🙂

pvinis11:07:41

is there a lib you would recommend for doing get and post requests with json, and saving cookies etc?

pvinis11:07:46

in clojurescript

pvinis11:07:57

cljsrn, but i guess its the same

akiroz11:07:02

not sure about react native but I'm using cljs-ajax with promesa for async requests and reagent-utils for various things. if react native offers the same APIs as browsers then it should work fine (I've never used react native before)

niwinz11:07:18

But I don't know if it works with react native.

akiroz11:07:07

^ that looks nice, I might use that in place of cljs-ajax + promesa 😄

pvinis11:07:12

now to find out how to include that in a re-natal project..

pesterhazy11:07:02

@pvinis: cookie are kept automatically by the react-native runtime, just like in the browser

pesterhazy11:07:31

at least on ios, but pretty on android too

pesterhazy11:07:51

cljs-ajax is awesome on rn