Skip to content

Commit 4300483

Browse files
authored
[0.81] Fix issue when creating a ContentIslandComponentView when the root ReactNativeIsland is not connected (#15650)
* Fix issue when creating a ContentIslandComponentView when the root ReactNativeIsland is not connected (#15649) * Fix issue when creating a ContentIslandComponentView when the root ReactNativeIsland is not connected * Change files * Unregister from state changed events after we have used it to connect * fix change files
1 parent cde7477 commit 4300483

7 files changed

Lines changed: 68 additions & 13 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"type": "prerelease",
2+
"type": "patch",
33
"comment": "Fix selectable text not working inside ScrollView",
44
"packageName": "react-native-windows",
55
"email": "74712637+iamAbhi-916@users.noreply.github.com",
66
"dependentChangeType": "patch"
7-
}
7+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"type": "none",
2+
"type": "patch",
33
"comment": "Fix memoryStram getting unitialized",
44
"packageName": "react-native-windows",
55
"email": "hmalothu@microsoft.com",
6-
"dependentChangeType": "none"
7-
}
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Fix issue when creating a ContentIslandComponentView when the root ReactNativeIsland is not connected",
4+
"packageName": "react-native-windows",
5+
"email": "30809111+acoates-ms@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"type": "none",
2+
"type": "patch",
33
"comment": "Fix crash in ABIViewManager::RemoveAllChildren during shutdown",
44
"packageName": "react-native-windows",
55
"email": "hmalothu@microsoft.com",
6-
"dependentChangeType": "none"
7-
}
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"type": "none",
2+
"type": "patch",
33
"comment": "visual studio 2026 strict check fix",
44
"packageName": "react-native-windows",
55
"email": "hmalothu@microsoft.com",
6-
"dependentChangeType": "none"
7-
}
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/ContentIslandComponentView.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ ContentIslandComponentView::ContentIslandComponentView(
4343
});
4444
}
4545

46-
void ContentIslandComponentView::OnMounted() noexcept {
46+
winrt::Microsoft::UI::Content::ContentIsland ContentIslandComponentView::ParentContentIsland() noexcept {
47+
auto root = rootComponentView();
48+
if (!root)
49+
return nullptr;
50+
return root->parentContentIsland();
51+
}
52+
53+
void ContentIslandComponentView::ConnectInternal() noexcept {
54+
if (!m_islandToConnect)
55+
return;
56+
4757
m_childSiteLink = winrt::Microsoft::UI::Content::ChildSiteLink::Create(
48-
rootComponentView()->parentContentIsland(),
58+
m_parentContentIsland,
4959
winrt::Microsoft::ReactNative::Composition::Experimental::CompositionContextHelper::InnerVisual(Visual())
5060
.as<winrt::Microsoft::UI::Composition::ContainerVisual>());
5161
m_childSiteLink.ActualSize({m_layoutMetrics.frame.size.width, m_layoutMetrics.frame.size.height});
@@ -69,6 +79,7 @@ void ContentIslandComponentView::OnMounted() noexcept {
6979
m_childSiteLink.Connect(m_islandToConnect);
7080
m_islandToConnect = nullptr;
7181
}
82+
UnregisterForRootIslandEvents();
7283

7384
ParentLayoutChanged();
7485
auto view = Parent();
@@ -85,12 +96,42 @@ void ContentIslandComponentView::OnMounted() noexcept {
8596
}
8697
}
8798

99+
void ContentIslandComponentView::RegisterForRootIslandEvents() noexcept {
100+
m_parentContentIsland = ParentContentIsland();
101+
102+
if (m_parentContentIsland.IsConnected()) {
103+
ConnectInternal();
104+
} else {
105+
m_islandStateChangedToken = m_parentContentIsland.StateChanged(
106+
[wkThis = get_weak()](
107+
const winrt::Microsoft::UI::Content::ContentIsland & /*island*/,
108+
const winrt::Microsoft::UI::Content::ContentIslandStateChangedEventArgs & /*args*/) {
109+
if (auto strongThis = wkThis.get()) {
110+
strongThis->ConnectInternal();
111+
}
112+
});
113+
}
114+
}
115+
116+
void ContentIslandComponentView::UnregisterForRootIslandEvents() noexcept {
117+
if (m_islandStateChangedToken) {
118+
m_parentContentIsland.StateChanged(m_islandStateChangedToken);
119+
m_islandStateChangedToken = {};
120+
m_parentContentIsland = nullptr;
121+
}
122+
}
123+
124+
void ContentIslandComponentView::OnMounted() noexcept {
125+
RegisterForRootIslandEvents();
126+
}
127+
88128
void ContentIslandComponentView::OnUnmounted() noexcept {
89129
m_layoutMetricChangedRevokers.clear();
90130
if (m_navigationHostDepartFocusRequestedToken && m_navigationHost) {
91131
m_navigationHost.DepartFocusRequested(m_navigationHostDepartFocusRequestedToken);
92132
m_navigationHostDepartFocusRequestedToken = {};
93133
}
134+
UnregisterForRootIslandEvents();
94135
}
95136

96137
void ContentIslandComponentView::ParentLayoutChanged() noexcept {

vnext/Microsoft.ReactNative/Fabric/Composition/ContentIslandComponentView.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,16 @@ struct ContentIslandComponentView : ContentIslandComponentViewT<ContentIslandCom
5858
void OnMounted() noexcept;
5959
void OnUnmounted() noexcept;
6060
void ParentLayoutChanged() noexcept;
61+
void ConnectInternal() noexcept;
62+
void RegisterForRootIslandEvents() noexcept;
63+
void UnregisterForRootIslandEvents() noexcept;
64+
winrt::Microsoft::UI::Content::ContentIsland ParentContentIsland() noexcept;
6165

6266
bool m_layoutChangePosted{false};
67+
winrt::Microsoft::UI::Content::ContentIsland m_parentContentIsland{nullptr};
6368
winrt::Microsoft::UI::Content::ContentIsland m_islandToConnect{nullptr};
69+
winrt::event_token m_islandStateChangedToken;
70+
6471
winrt::event_token m_mountedToken;
6572
winrt::event_token m_unmountedToken;
6673
std::vector<winrt::Microsoft::ReactNative::ComponentView::LayoutMetricsChanged_revoker> m_layoutMetricChangedRevokers;

0 commit comments

Comments
 (0)