Skip to content

Commit f9dafc0

Browse files
aashay-gaikwadfacebook-github-bot
authored andcommitted
Spec to KComponent -> ParentComponentSendsEventToChild and ChildComponentReceivesEventFromParent
Summary: Updating LithoDocs for Communicating between Components to have KComponent samples: https://fblitho.com/docs/mainconcepts/coordinate-state-actions/communicating-between-components/ Suggesting usage of [Controllers Pattern](https://fburl.com/rnd54nkv) in case of triggering an event on a child in case of KComponents. Not deleting the Spec files as they're used for Java Examples Reviewed By: astreet Differential Revision: D73110218 fbshipit-source-id: 5522ef975721491d2d0d322c2cfa4b4452d8a2bc
1 parent 234c674 commit f9dafc0

4 files changed

Lines changed: 169 additions & 17 deletions

File tree

docs/mainconcepts/coordinate-state-actions/communicating-between-components.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,17 @@ When the data is updated as a result of an action controlled by the parent compo
3131

3232
The following code illustrates this concept with a click event on the parent component.
3333

34-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentSendsEventToChildSpec.java start=start_update_prop end=end_update_prop
34+
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentSendsEventToChildKComponent.kt start=start_update_prop end=end_update_prop
3535
```
3636

3737
### Triggering an Action on a child from a parent
3838

39-
There are cases when a parent needs to trigger an action on a child instead of just passing new data. To do this, the parent needs to keep a reference to the child and trigger an action on it using that reference.
39+
There are cases when a parent needs to trigger an action on a child instead of just passing new data. To do this, the parent can interact with the child using controllers (see [Controllers Pattern](/docs/mainconcepts/primitivecomponents/primitive-controllers/)),
40+
which the parent creates and passes to the child component as a prop:
4041

41-
The reference to the child is maintained through a `Handle` instance, which the parent creates and passes to the child component as a prop:
42-
43-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentSendsEventToChildSpec.java start=start_define_handle end=end_define_handle
44-
```
45-
46-
The parent uses the Handle reference to trigger an action on the child component:
47-
48-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentSendsEventToChildSpec.java start=start_trigger end=end_trigger
42+
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentSendsEventToChildKComponent.kt start=start_define_controller end=end_define_controller
4943
```
5044

51-
The action is defined on the child component using the `@OnTrigger` annotation in Java:
52-
53-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ChildComponentReceivesEventFromParentSpec.java start=start_define_trigger end=end_define_trigger
54-
```
55-
56-
Defining triggers in `KComponents` is not supported yet, but they can invoke triggers as with Java Components.
57-
5845
### Communicating between siblings
5946

6047
Two sibling components (two child components of the same parent) cannot communicate directly. All communication must flow through the parent component, which intercepts events from a child component and notifies other child components of those events (using the methods detailed above).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.samples.litho.java.communicating
18+
19+
import android.widget.Toast
20+
import com.facebook.litho.Column
21+
import com.facebook.litho.Component
22+
import com.facebook.litho.ComponentContext
23+
import com.facebook.litho.ComponentScope
24+
import com.facebook.litho.KComponent
25+
import com.facebook.litho.kotlin.widget.Text
26+
import com.facebook.litho.onCleanup
27+
import com.facebook.litho.useEffect
28+
import com.facebook.rendercore.dp
29+
30+
class ChildComponentReceivesEventFromParentKComponent(
31+
private val controller: ParentToChildEventController,
32+
private val textFromParent: String,
33+
) : KComponent() {
34+
35+
override fun ComponentScope.render(): Component? {
36+
useEffect(controller) {
37+
controller.setTriggerShowToastListener { triggerShowToast(context, it) }
38+
onCleanup { controller.reset() }
39+
}
40+
41+
return Column {
42+
child(Text(text = "ChildComponent", textSize = 20.dp))
43+
child(Text(text = "Text received from parent: $textFromParent", textSize = 15.dp))
44+
}
45+
}
46+
47+
private fun triggerShowToast(c: ComponentContext, message: String) {
48+
Toast.makeText(c.androidContext, message, Toast.LENGTH_SHORT).show()
49+
}
50+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.samples.litho.java.communicating
18+
19+
import android.graphics.Color
20+
import com.facebook.litho.Column
21+
import com.facebook.litho.Component
22+
import com.facebook.litho.ComponentScope
23+
import com.facebook.litho.KComponent
24+
import com.facebook.litho.Style
25+
import com.facebook.litho.core.margin
26+
import com.facebook.litho.core.padding
27+
import com.facebook.litho.flexbox.border
28+
import com.facebook.litho.kotlin.widget.Border
29+
import com.facebook.litho.kotlin.widget.BorderEdge
30+
import com.facebook.litho.kotlin.widget.BorderRadius
31+
import com.facebook.litho.kotlin.widget.Text
32+
import com.facebook.litho.useCached
33+
import com.facebook.litho.useState
34+
import com.facebook.litho.view.onClick
35+
import com.facebook.rendercore.dp
36+
37+
class ParentComponentSendsEventToChildKComponent : KComponent() {
38+
39+
// start_define_controller
40+
override fun ComponentScope.render(): Component? {
41+
42+
val controller = useCached { ParentToChildEventController() }
43+
val version = useState { 0 }
44+
45+
return Column(style = Style.padding(all = 30.dp)) {
46+
child(Text(text = "ParentComponent", textSize = 30.dp))
47+
child(
48+
Text(
49+
text = "Click to trigger show toast event on ChildComponent",
50+
textSize = 15.dp,
51+
style =
52+
Style.padding(all = 5.dp)
53+
.margin(top = 15.dp)
54+
.border(
55+
Border(
56+
edgeAll = BorderEdge(color = Color.BLACK, width = 1.dp),
57+
radius = BorderRadius(all = 2.dp)))
58+
.onClick { controller.trigger("Message from parent") }))
59+
child(
60+
ChildComponentReceivesEventFromParentKComponent(
61+
controller = controller, textFromParent = "Child with controller"))
62+
// end_define_controller
63+
// start_update_prop
64+
child(
65+
Text(
66+
text = "Click to send new text to ChildComponent",
67+
textSize = 15.dp,
68+
style =
69+
Style.padding(all = 5.dp)
70+
.margin(top = 15.dp)
71+
.border(
72+
Border(
73+
edgeAll = BorderEdge(color = Color.BLACK, width = 1.dp),
74+
radius = BorderRadius(all = 2.dp)))
75+
.onClick { version.update { it + 1 } }))
76+
child(
77+
ChildComponentReceivesEventFromParentKComponent(
78+
controller = controller, textFromParent = "Version ${version.value}"))
79+
// end_update_prop
80+
}
81+
}
82+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.samples.litho.java.communicating
18+
19+
class ParentToChildEventController {
20+
private var observer: ((String) -> Unit)? = null
21+
22+
fun setTriggerShowToastListener(observer: (String) -> Unit) {
23+
this.observer = observer
24+
}
25+
26+
fun trigger(message: String) {
27+
observer?.invoke(message)
28+
}
29+
30+
fun reset() {
31+
observer = null
32+
}
33+
}

0 commit comments

Comments
 (0)