Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 1885738

Browse files
Update "Building a Full Stack Web App with Kotlin Multiplatform" to latest Kotlin and Kotlin-React DSL
1 parent c9f116c commit 1885738

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

Full Stack Web App with Kotlin Multiplatform/02_Project_Setup.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Like all Kotlin projects targeting more than one platform, our project uses the
2121

2222
```kotlin
2323
plugins {
24-
kotlin("multiplatform") version "1.4.0"
24+
kotlin("multiplatform") version "1.6.10"
2525
application //to run JVM part
26-
kotlin("plugin.serialization") version "1.4.0"
26+
kotlin("plugin.serialization") version "1.6.10"
2727
}
2828
```
2929

Full Stack Web App with Kotlin Multiplatform/05_Building_the_Frontend.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,36 @@ Instead of rendering a simple "Hello, Kotlin/JS" string, it's time we make our a
5858
Replace the content inside `src/jsMain/kotlin/Main.kt` with the following:
5959

6060
```kotlin
61-
import react.child
6261
import react.dom.render
6362
import kotlinx.browser.document
63+
import react.create
6464

6565
fun main() {
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)
6968
}
7069
```
7170

7271
#### Building and rendering the shopping list
7372

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
7574

7675
- keep the "local state" of the shopping list (which elements to display),
7776
- load the shopping list elements from the server (and set the state accordingly), and
7877
- provide React with instructions on how to render the list.
7978

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`:
8180

8281
```kotlin
8382
import react.*
84-
import react.dom.*
85-
import kotlinext.js.*
86-
import kotlinx.html.js.*
8783
import kotlinx.coroutines.*
84+
import react.dom.html.ReactHTML.h1
85+
import react.dom.html.ReactHTML.li
86+
import react.dom.html.ReactHTML.ul
8887

8988
private val scope = MainScope()
9089

91-
val app = fc<Props> { _ ->
90+
val App = FC<Props> {
9291
var shoppingList by useState(emptyList<ShoppingListItem>())
9392

9493
useEffectOnce {
@@ -129,48 +128,50 @@ In order to receive input from the user, we need an input component which provid
129128
Create the file `src/jsMain/kotlin/InputComponent.kt`, and fill it with the following definition:
130129

131130
```kotlin
131+
import org.w3c.dom.HTMLFormElement
132132
import react.*
133-
import react.dom.*
134-
import kotlinx.html.js.*
135-
import kotlinx.html.InputType
136-
import org.w3c.dom.events.Event
137133
import org.w3c.dom.HTMLInputElement
134+
import react.dom.events.ChangeEventHandler
135+
import react.dom.events.FormEventHandler
136+
import react.dom.html.InputType
137+
import react.dom.html.ReactHTML.form
138+
import react.dom.html.ReactHTML.input
138139

139140
external interface InputProps : Props {
140141
var onSubmit: (String) -> Unit
141142
}
142143

143-
val inputComponent = fc<InputProps> { props ->
144+
val InputComponent = FC<InputProps> { props ->
144145
val (text, setText) = useState("")
145146

146-
val submitHandler: (Event) -> Unit = {
147+
val submitHandler: FormEventHandler<HTMLFormElement> = {
147148
it.preventDefault()
148149
setText("")
149150
props.onSubmit(text)
150151
}
151152

152-
val changeHandler: (Event) -> Unit = {
153-
val value = (it.target as HTMLInputElement).value
154-
setText(value)
153+
val changeHandler: ChangeEventHandler<HTMLInputElement> = {
154+
setText(it.target.value)
155155
}
156156

157157
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
162163
}
163164
}
164165
}
165166
```
166167

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).
168169

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):
170171

171172
```kotlin
172-
child(inputComponent) {
173-
attrs.onSubmit = { input ->
173+
InputComponent {
174+
onSubmit = { input ->
174175
val cartItem = ShoppingListItem(input.replace("!", ""), input.count { it == '!' })
175176
scope.launch {
176177
addShoppingListItem(cartItem)
@@ -195,7 +196,7 @@ Rather than add another UI element (like a "delete" button), we can just modify
195196
In `src/jsMain/kotlin/App.kt`, add the following to the `li` block (inside the `ul` block):
196197

197198
```kotlin
198-
attrs.onClickFunction = {
199+
onClick = {
199200
scope.launch {
200201
deleteShoppingListItem(item)
201202
shoppingList = getShoppingList()

0 commit comments

Comments
 (0)