1

Is it possible to propagate an event from an nth-child to the root parent?

I have been looking out for a good guide that does not involve declaring the event at every node to the root of the tree. My ultimate objective is to expose and event to outside litho and on to Android Kotlin/Java.

I'm seeing this in Litho Events guide but I'm a bit confused as to what this means. Does this mean it can't be done? Or does this mean passing data to sibling can't be done?

enter image description here

Bangonkali
  • 1,111
  • 9
  • 13

1 Answers1

0

You have the option of using @TreeProps which expose a prop to the entire subtree for a component. We generally don't recommend this approach because it can make the dependencies of your Component hard to reason about -- for example if you want to re-use that Component in another tree, it's not clear from the creator of that component that there's a contract that the Component must be hosted in a subtree that defines that specific @TreeProp.

The warning you screenshot is not really correct, I will work on editing it or removing it.

Foxichu
  • 952
  • 10
  • 22
  • Hi @Foxichu do you have any advice in propagating custom events that are annotated by `@Event` from let's say the 10th child without cascading the event from parent to parent? I'm not quite sure how I can use `@TreeProp` for this. My objective is to be able to have just one eventHandler in the root component that is exposed to Android Kotlin. I'm then passing immutable data back to the root to rerender everything if the event triggers a state change. Basically state is handled outside of the Root Component in my app. – Bangonkali Nov 25 '19 at 16:31
  • If you're talking about a parent putting an EventHandler on a deep child, you can put the EventHandler in a TreeProp which will then be accessible by the child. – Foxichu Nov 26 '19 at 15:23
  • Hi @Foxichu, i would like to solicit your idea if this is a reasonable approach to passing events using a treeProp from a very deep child to the root while skipping all intermediate nodes in between https://github.com/bangonkali/litho/tree/feature/codelab/treeprop_events/codelabs/tree-prop-events I created this pattern as a guide for myself but unfortunately, I have not seen other patterns, that solve this particular problem. Maybe my implementation of your advice is wrong or you had other thought on how it should be? – Bangonkali Nov 27 '19 at 22:29
  • 1
    Yep, that's what I was referring to :) The general pattern has the issue I mentioned above about implicit coupling but otherwise it will work. If you're looking for more feedback on the approach, this is exactly analogous to using React's context so you can look at discussion around that! https://reactjs.org/docs/context.html – Foxichu Nov 29 '19 at 10:23