nitro icon indicating copy to clipboard operation
nitro copied to clipboard

feat: Add support for Views with children

Open patrickkabwe opened this issue 9 months ago • 12 comments

Closes #873 This PR adds ability for Nitro views to render React children on Android.

By default, Nitro Views uses SimpleViewManager to handle views. To support children, this PR introduces a new ViewConfig prop to the HybridView type, which extends HybridViewConfig. The ViewConfig currently supports an allowChildren flag. When allowChildren is set to true, it switches the underlying view manager to a ReactViewManager on Android, enabling child view rendering.

Screenshots

iOS Android
iOS Android

patrickkabwe avatar May 01 '25 14:05 patrickkabwe

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nitro-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 16, 2025 9:19am

vercel[bot] avatar May 01 '25 14:05 vercel[bot]

@mrousavy PR is ready for review

patrickkabwe avatar Jun 16 '25 12:06 patrickkabwe

To make it work properly on iOS, (un)mountChildComponentView should be passed thru to contentView:

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol>*)childComponentView
                          index:(NSInteger)index {
    [self.contentView mountChildComponentView:childComponentView index:index];
}

- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol>*)childComponentView
                            index:(NSInteger)index {
    [self.contentView unmountChildComponentView:childComponentView index:index];
}

In current implementation children views is rendering as subviews of parent RCTViewComponentView, under module's view. Because of this we can't control the rendering of children in the module's swift code.

This could be tested by adding background color to UIView:

override public init() {
  view.backgroundColor = .red
}

It will overlay all of its children.

wielski avatar Jun 23 '25 17:06 wielski

To make it work properly on iOS, (un)mountChildComponentView should be passed thru to contentView:

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol>*)childComponentView
                          index:(NSInteger)index {
    [self.contentView mountChildComponentView:childComponentView index:index];
}

- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol>*)childComponentView
                            index:(NSInteger)index {
    [self.contentView unmountChildComponentView:childComponentView index:index];
}

In current implementation children views is rendering as subviews of parent RCTViewComponentView, under module's view. Because of this we can't control the rendering of children in the module's swift code.

This could be tested by adding background color to UIView:

override public init() {
  view.backgroundColor = .red
}

It will overlay all of its children.

Thanks for sharing i will take a look at this.

patrickkabwe avatar Jun 24 '25 06:06 patrickkabwe

Hi @mrousavy and @patrickkabwe

Thanks for the work on adding child view support, @patrickkabwe . This feature would be really helpful for my project. I wanted to kindly check if there are any updates on this PR or if there’s another recommended way to enable child support in Nitro in the meantime?

Appreciate your time!

Swami-Laxmikant avatar Sep 13 '25 20:09 Swami-Laxmikant

if there’s another recommended way to enable child support in Nitro in the meantime?

You can just edit your nitro-generated *ViewManager.kt file and change SimpleViewManager -> ViewGroupManager, and View -> ViewGroup. Then it automatically works.

We will add this to nitrogen in the future, but for now this is your safe workaround. :)

On iOS it already works.

mrousavy avatar Sep 15 '25 11:09 mrousavy

@patrickkabwe is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Sep 20 '25 20:09 vercel[bot]

@mrousavy PR is ready for review. please take a look once you get some time

patrickkabwe avatar Sep 20 '25 20:09 patrickkabwe

I found some code structure changes. The idea is good, but execution could be improved

Thanks for the feedback. I’ve addressed the concerns you raised please take another look.

patrickkabwe avatar Sep 23 '25 07:09 patrickkabwe

@mrousavy Is there anything outstanding on this PR aside from the recent merge conflicts? I’m currently developing a library that relies on this functionality, maintaining local patches has been workable but isn’t sustainable long-term. Please let me know if there are any remaining issues so I can allocate time to address them.

patrickkabwe avatar Oct 27 '25 12:10 patrickkabwe

hey - yea sorry I want this feature but there's a ton of conflicts.

I think I will move this to a separate PR and start the work there, I have some minor code organization changes that will probably just be quicker if I do the refactors myself - is that cool with you?

mrousavy avatar Oct 27 '25 12:10 mrousavy

hey - yea sorry I want this feature but there's a ton of conflicts.

I think I will move this to a separate PR and start the work there, I have some minor code organization changes that will probably just be quicker if I do the refactors myself - is that cool with you?

Yes this sounds good

patrickkabwe avatar Oct 27 '25 12:10 patrickkabwe