Fork me on GitHub
#cljsrn
<
2017-01-17
>
tianshu06:01:25

It's there a way for reagent to create a real react component? some component receive component or element as props, for example, RefreshControl. I can't make it work with reagent.

ejelome07:01:27

^ I think that's where adapt-react-class comes into play, and I think you should think in reverse, e.g. convert the react component to work with reagent's hiccup-like forms

ejelome07:01:22

maybe you can back read here, probably it answers some of the questions (e.g. use adapt-react-class for importing react components, and as-element to return a component) https://clojurians.slack.com/archives/cljsrn/p1484501499001949

ejelome07:01:34

^ click the link so you can read the discussion, after that link are the answers and clarifications

ejelome22:01:52

How do you handle a json request in re-natal? I'm getting a blank response ...

(def data (js->clj GET ""))

(defn app-root []
  [text (:title data)])

ejelome22:01:54

^ I simply want to get the value of the title from the json data

peterschwarz22:01:20

@ejelome You either need to pass :keywordize-keys true to js->clj or you can just get the title by string:

; Add this to your requires
(require [goog.object :as obj])

; ... fetch your data
(defn app-root []
  [text (obj/get data "title")])

peterschwarz22:01:47

With the later, you can avoid converting the js->clj, which, as I understand it, can be slow for large/deep objects

ejelome22:01:21

w8 a moment, will try

ejelome22:01:35

still blank 😐

petterik22:01:33

Are you missing a parens that wraps (GET ...), @ejelome?

ejelome22:01:48

if I wrap it in parens, it causes an error

petterik22:01:38

What's the error you're getting? What is the GET symbol referred to?

ejelome22:01:25

GET comes from [ajax.core :as [GET]]

ejelome22:01:29

here's the full code just in case:

(ns networking.android.core
  (:require [reagent.core :as r]
            [ajax.core :refer [GET]]
            [goog.object :as obj]))

(def ReactNative (js/require "react-native"))

(def app-registry (.-AppRegistry ReactNative))
(def text (r/adapt-react-class (.-Text ReactNative)))

(def data (GET ""))

(defn app-root []
  [text (obj/get data "title")])

(defn init []
  (.registerComponent app-registry "Networking" #(r/reactify-component app-root)))

artemyarulin22:01:30

I don’t think that HTTP call is sync

artemyarulin22:01:18

most probably you need to pass callback to GET function or at least add some dummy setTimeout

ejelome22:01:54

hmmm, ok will try a handler

artemyarulin22:01:25

read the docs, I’m not aware about ajax.core at all but JS as a platform doesn’t allow doing sync HTTP requests so most probably callback has to passed somewhere

ejelome22:01:14

ok, that one worked

ejelome22:01:36

I actually read the docs of it but getting it mixed with components becomes a challenge

ejelome22:01:44

(defn handler [response] (str response))

(def data (clj->js GET "" {:handler handler}))

(defn app-root []
  [text (str data)])

artemyarulin22:01:13

well, you handler function doesn’t do anything

ejelome22:01:18

it does, but only prints it, it's just to know if it receives something (which it is, it received a stringified callback)

artemyarulin22:01:38

ah, ok. Now just figure how to pass updated data to reagent and it’s done 🙂

ejelome22:01:51

unfortunately I need to sleep now, oh well, sleep time

artemyarulin22:01:27

many time it helps 🙂

ejelome22:01:25

yeah, clojure is like my past time after work, probably too tired to figure things out XD

petterik23:01:59

@ejelome (str response) only turns the response into a string. Use prn if you want to print it: (defn handler [response] (prn (str response)))