You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 17, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: Full Stack Web App with Kotlin Multiplatform/05_Building_the_Frontend.md
+29-28Lines changed: 29 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,37 +58,36 @@ Instead of rendering a simple "Hello, Kotlin/JS" string, it's time we make our a
58
58
Replace the content inside `src/jsMain/kotlin/Main.kt` with the following:
59
59
60
60
```kotlin
61
-
importreact.child
62
61
importreact.dom.render
63
62
importkotlinx.browser.document
63
+
importreact.create
64
64
65
65
funmain() {
66
-
render(document.getElementById("root")) {
67
-
child(app)
68
-
}
66
+
val container = document.getElementById("root") ?: error("Couldn't find container!")
67
+
render(App.create(), container)
69
68
}
70
69
```
71
70
72
71
#### Building and rendering the shopping list
73
72
74
-
Next up is the implementation for the `app` component we would like to render. For our shopping list application, we need to
73
+
Next up is the implementation for the `App` component we would like to render. For our shopping list application, we need to
75
74
76
75
- keep the "local state" of the shopping list (which elements to display),
77
76
- load the shopping list elements from the server (and set the state accordingly), and
78
77
- provide React with instructions on how to render the list.
79
78
80
-
Based on these requirements, we can implement the `app` component as follows. We create and fill the file `src/jsMain/kotlin/App.kt`:
79
+
Based on these requirements, we can implement the `App` component as follows. We create and fill the file `src/jsMain/kotlin/App.kt`:
81
80
82
81
```kotlin
83
82
importreact.*
84
-
importreact.dom.*
85
-
importkotlinext.js.*
86
-
importkotlinx.html.js.*
87
83
importkotlinx.coroutines.*
84
+
importreact.dom.html.ReactHTML.h1
85
+
importreact.dom.html.ReactHTML.li
86
+
importreact.dom.html.ReactHTML.ul
88
87
89
88
privateval scope =MainScope()
90
89
91
-
valapp=fc<Props> { _ ->
90
+
valApp=FC<Props> {
92
91
var shoppingList by useState(emptyList<ShoppingListItem>())
93
92
94
93
useEffectOnce {
@@ -129,48 +128,50 @@ In order to receive input from the user, we need an input component which provid
129
128
Create the file `src/jsMain/kotlin/InputComponent.kt`, and fill it with the following definition:
130
129
131
130
```kotlin
131
+
importorg.w3c.dom.HTMLFormElement
132
132
importreact.*
133
-
importreact.dom.*
134
-
importkotlinx.html.js.*
135
-
importkotlinx.html.InputType
136
-
importorg.w3c.dom.events.Event
137
133
importorg.w3c.dom.HTMLInputElement
134
+
importreact.dom.events.ChangeEventHandler
135
+
importreact.dom.events.FormEventHandler
136
+
importreact.dom.html.InputType
137
+
importreact.dom.html.ReactHTML.form
138
+
importreact.dom.html.ReactHTML.input
138
139
139
140
externalinterfaceInputProps : Props {
140
141
var onSubmit: (String) ->Unit
141
142
}
142
143
143
-
valinputComponent=fc<InputProps> { props ->
144
+
valInputComponent=FC<InputProps> { props ->
144
145
val (text, setText) = useState("")
145
146
146
-
val submitHandler:(Event) ->Unit= {
147
+
val submitHandler:FormEventHandler<HTMLFormElement>= {
147
148
it.preventDefault()
148
149
setText("")
149
150
props.onSubmit(text)
150
151
}
151
152
152
-
val changeHandler: (Event) ->Unit= {
153
-
val value = (it.target asHTMLInputElement).value
154
-
setText(value)
153
+
val changeHandler:ChangeEventHandler<HTMLInputElement> = {
154
+
setText(it.target.value)
155
155
}
156
156
157
157
form {
158
-
attrs.onSubmitFunction = submitHandler
159
-
input(InputType.text) {
160
-
attrs.onChangeFunction = changeHandler
161
-
attrs.value = text
158
+
onSubmit = submitHandler
159
+
input {
160
+
type =InputType.text
161
+
onChange = changeHandler
162
+
value = text
162
163
}
163
164
}
164
165
}
165
166
```
166
167
167
-
While an in-depth explanation of how this component works is outside the scope of this hands-on, it can be summarized as follows: The `inputComponent` keeps track of its internal state (what the user has typed so far), and exposes an `onSubmit` handler that gets called when the user submits the form (usually by pressing the `Enter` key).
168
+
While an in-depth explanation of how this component works is outside the scope of this hands-on, it can be summarized as follows: The `InputComponent` keeps track of its internal state (what the user has typed so far), and exposes an `onSubmit` handler that gets called when the user submits the form (usually by pressing the `Enter` key).
168
169
169
-
To use this `inputComponent` from our application. we add the following snippet to `src/jsMain/kotlin/App.kt`, at the bottom of the `functionalComponent` block (after the closing brace for the `ul` element):
170
+
To use this `InputComponent` from our application. we add the following snippet to `src/jsMain/kotlin/App.kt`, at the bottom of the `FC` block (after the closing brace for the `ul` element):
170
171
171
172
```kotlin
172
-
child(inputComponent) {
173
-
attrs.onSubmit = { input ->
173
+
InputComponent {
174
+
onSubmit = { input ->
174
175
val cartItem =ShoppingListItem(input.replace("!", ""), input.count { it =='!' })
175
176
scope.launch {
176
177
addShoppingListItem(cartItem)
@@ -195,7 +196,7 @@ Rather than add another UI element (like a "delete" button), we can just modify
195
196
In `src/jsMain/kotlin/App.kt`, add the following to the `li` block (inside the `ul` block):
0 commit comments