• No results found

Frontend- Entwicklung mit React/Reacl. Michael

N/A
N/A
Protected

Academic year: 2021

Share "Frontend- Entwicklung mit React/Reacl. Michael"

Copied!
50
0
0

Loading.... (view fulltext now)

Full text

(1)

Frontend-­‐Entwicklung  mit    

React/Reacl  

Michael  Sperber  

@sperbsen  

(2)

Individualsoftware  

branchenunabhängig  

Scala,  Clojure,  Erlang,  Haskell,  F#,  Swift  

Schulungen,  Coaching  

www.active-­‐group.de

 

(3)
(4)
(5)

MVC  

Modell  

(6)

Problem  

Modell  

View  

Modell’  

View’  

(7)
(8)

Best  of  JavaFX  

javafx.application.Platform.runLater(

!

new Runnable {

!

override def run() {

!

val webView =

!

new javafx.scene.web.

WebView

()

!

val webEngine = webView.

getEngine

!

val mainURL = getClass.getResource

!

(

”/index.html"

)

!

webEngine.load(mainURL.toExternalForm)

!

(9)

Wo  läuft  JavaScript/HTML5?  

Browser  

Mobile  

JavaFX  

Windows-­‐Apps  

QT  

Gtk  

(10)
(11)

Was  ist  so  schlimm  an  JavaScript?  

JavaScript  

(12)

Reacl  

ClojureScript  

(13)

Reacl  

Modell  

View  

neues  

Modell  

neuer  

View  

Änderung   generiert   generiert   User  
(14)

Rendering  in  React  

virtuelles  DOM  

(15)

Die  allgegenwärtige  

Todo-­‐App  

(16)

Lisp  

funktional  

(17)

Clojure  

15

!

true

!

false

!

"foo"

!

(

+

1 2

)!

(

+

1

(

*

2

!

(

+

17 21

)))!

(18)

Clojure  

(

def

pi

3.14159265

)!

!

(

defn

circumference!

[r]!

(

*

2

pi r))!

(19)
(20)

Applikations-­‐Zustand  

(defrecord Todo

[id text done?])

(def t1 (->Todo 0 “Make money” false)

(def t2 (->Todo 1 “retire” false)

(defrecord TodosApp

[next-id todos])

(21)

Einzelnes  Todo  

(

reacl/defclass

to-do-item

!

this todo []

!

render

!

(

dom/div

!

(

dom/input

{

:type

"checkbox"

!

:value

(

:done?

todo)})

!

(

dom/button

"Zap")

!

(

:text

todo)))

!

 

(22)

Todo  abhaken  

(reacl/defclass to-do-item

!

this todo []

!

render

!

(dom/div

!

(dom/input {

:type

"checkbox"

!

:value

(

:done?

todo)

!

:onChange!

(fn [_] ... )})

!

(dom/button

"Zap"

)

!

(

:text

todo)))

!

(23)

Todo  abhaken  

(reacl/defclass to-do-item!

this todo []!

render!

(dom/div!

(dom/input {:type "checkbox"!

:value (:done? todo)!

:onChange! (fn [_]! (reacl/send-message! ! this! ...))})! (dom/button "Zap")! (:text todo)))!  

(24)

Todo  abhaken  

(reacl/defclass to-do-item!

this todo []!

render!

(dom/div!

(dom/input {:type "checkbox"!

:value (:done? todo)!

:onChange!

(fn [_]!

(reacl/send-message! ! this!

(.-checked (dom/dom-node this ...)))})!

(dom/button "Zap")!

(:text todo)))!

(25)

Todo  abhaken  

(reacl/defclass to-do-item! this todo []! render! (dom/letdom! [checkbox (dom/input! {:type "checkbox"!

:value (:done? todo)!

:onChange!

(fn [_]!

(reacl/send-message! !

this!

(.-checked (dom/dom-node this checkbox))))})]!

(dom/div checkbox!

(dom/button "Zap")!

(26)

Checkbox-­‐Message  behandeln  

(reacl/defclass to-do-item! this todo []! render! (dom/letdom! [checkbox (dom/input! {:type "checkbox"!

:value (:done? todo)!

:onChange!

(fn [_]!

(reacl/send-message! !

this!

(.-checked (dom/dom-node this checkbox))))})]!

(dom/div checkbox! (dom/button "Zap")! (:text todo)))! handle-message! (fn [checked?]! (reacl/return :app-state!

(27)

Checkbox-­‐Message  behandeln  

handle-message!

(

fn

[checked?]!

(

reacl/return

!

:app-state

!

(

assoc

todo

:done?

!

checked?)))!

(reacl/send-message! !

this!

(28)

Reine  Funktionen  

id 5

text retire

done? false

(

assoc :done? true

)

 

id 5

text retire

done? true

(29)

Todo  löschen  

(dom/button

"Zap"

)

 

(30)

Todo  löschen  

(

defrecord

Delete [todo])!

(

dom/button

!

{

:onClick

!

(

fn

[_]!

(

reacl/send-message!

!

parent (

->Delete

todo)))}!

"Zap"

)!

!

(31)

Parent-­‐Parameter  

(

reacl/defclass

to-do-item!

this todo [parent]

 

           ...

)!

(32)

Unveränderbare  Daten  

[  

]  

(33)
(34)

Lokaler  Zustand  

local  

state  

(35)

Applikations-­‐Zustand,  

lokaler  Zustand  

(

reacl/defclass

to-do-app!

this app-state local-state []!

initial-state “”!

render!

(

dom/div

!

(

dom/h3

"TODO"

)!

...))!

local  

state  

app  

state  

(36)

Todo-­‐App  

(

defrecord

TodosApp

!

[next-id todos])

!

!

(

defrecord

NewText [text])

!

(

defrecord

Submit [])

!

(

defrecord

Change [todo])

!

(

defrecord

Delete [todo])

!

!

!

!

(37)

Todo-­‐App  

(reacl/defclass to-do-app!

this app-state local-state []!

render!

(dom/div!

(dom/h3 "TODO")!

(dom/div !

(map (fn [todo]!

(dom/keyed (str (:id todo))!

(to-do-item! todo!

(reacl/reaction this ->Change)!

this)))!

(:todos app-state)))!

(38)

Reacl-­‐Komponente  instanzieren  

(

to-do-item

!

todo!

(

reacl/reaction

this ->Change)!

this))

 

(

reacl/defclass

to-do-item!

this todo [parent]!

...)!

(39)

Reaktionen  

(

reacl/reaction

this ->Change)!

!

(40)

Unveränderbare  Daten  

[  

]  

(41)

Todo  ändern  

handle-message! (fn [msg]! (cond! ...! (instance? Change msg)!

(let [changed-todo (:todo msg)!

changed-id (:id changed-todo)]!

(reacl/return :app-state!

(assoc app-state!

:todos (map (fn [todo]!

(if (= changed-id (:id todo))!

changed-todo!

todo))!

(42)
(43)

Neue  Todos  

(reacl/defclass to-do-app!

this app-state local-state []!

render! (dom/div! ...! (dom/form! {:onSubmit (fn [e]! (.preventDefault e)!

(reacl/send-message! this (->Submit)))}!

...!

(dom/button!

(44)

 Neue  Todos  

handle-message! (fn [msg]! (cond! ... ! (instance? Submit msg)!

(let [next-id (:next-id app-state)]!

(reacl/return :local-state ""!

:app-state!

(assoc app-state!

:todos!

(concat (:todos app-state)!

[(->Todo next-id!

local-state false)])!

(45)

Text  für  neues  Todo  

(reacl/defclass to-do-app!

this app-state local-state []!

render! (dom/div! ...! (dom/form! ...! (dom/input {:onChange ! (fn [e]! (reacl/send-message!! this! (->NewText! (.. e -target -value))))! :value local-state})))))!

(46)

Text  für  neues  Todo  

handle-message!

(

fn

[msg]!

(

cond

!

...!

(

instance?

NewText msg)!

(

reacl/return

:local-state

!

(

:text

msg))))!

(47)

Komponenten-­‐Baum  

Argumente,  

App-­‐Zustand  

lokaler   Zustand  

Argumente,  

App-­‐Zustand  

App-­‐Zustand  

Argumente,  

App-­‐Zustand  

Message-­‐

Handler   Message-­‐Handler   Reaktion  

(48)

React/Reacl  

JavaFX  

Design  mit  HTML5/CSS  

ClojureScript  statt  JavaScript  

[Clojure  statt  Java]  

unveränderliche  Daten  &  React  statt  

DOM-­‐Manipulation  

(49)

Clojure/ClojureScript/Reacl  

mehr  Infos:  

(50)

Todo  löschen  

handle-message! (fn [msg]! (cond! ...! (instance? Delete msg)!

(let [id (:id (:todo msg))]!

(reacl/return :app-state!

(assoc app-state!

:todos !

(remove (fn [todo] (= id (:id todo)))!

(:todos app-state)))))!

References

Related documents

X-ray diffraction UV-vis absorption UV-vis emission UV-vis time decay micro-Raman mid-FTIR near-FTIR NMR-relaxometer AFM video microscopy UV-vis fluo imaging

It happens when the change in the variable profit P for the enrolled area equalling the eligible area does not cover the additional fixed costs due to the contract.. On the

For each of the three most recent years, the number of core faculty receiving external funds, average external funds per faculty, and total external funds per program per

In 2017, the UN Security Council adopted Resolution 2347, lauded as “a historic milestone” in the international struggle to safeguard cultural heritage in armed conflict. Among a wide

B) This sub-unit will be used to begin the ‘War Unit’ portion of the curriculum. It will be used to introduce the concept of war and explain to students how the foreign policy

The principal components (PCs) that accounted for $ 80% of the variance were used in subsequent analyses. The resultant factor scores from the PCA, plus the wiggle-dive variable,

 Detail Oriented  Flexibility  Computer Skills  Technical Skills  Interpersonal Skills  Organization  Creativity  Strategic.

search terms included: music, music therapy, young people, adolescents, adults, children, paediatric intensive care, picu, icu, critical care, mechanical ventilation and