Skip to content

Commit 4a48ccf

Browse files
authored
Fix issue when emptying nav stack on Windows (#1890)
## Description When using a `StackNavigator` on an app on Windows, when you popped the stack you would end up with no displayed content instead of the first page. ## Changes The Windows stack implementation wasn't updating `Content` to the top of the stack (after the removal and motification of the vector). Also the Windows project had an aggressive assertion of SDK version, causing apps to unnecessarily either download an older SDK or patch their app. See react-native-async-storage/async-storage#810 for a similar fix. ## Test code and steps to reproduce ```jsx import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; // ,,, const Stack = createNativeStackNavigator(); export default function App() : JSX.Element { return ( <NavigationContainer> <Stack.Navigator initialRouteName='Home'> <Stack.Screen name='Home' component={HomeScreen} /> <Stack.Screen name='Details' component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); } ``` Somewhere else ```jsx navigation.navigate('Details'); ``` And then later ```jsx navigation.goBack(); ``` Result: rendering nothing instead of the 'Home' page. Yep, that's all it took. As far as I can tell this was broken since implementation, but the usage I know of on Windows was using Drawer and that part's all fine. ## Checklist - [x] Included code example that can be used to test this change - [x] Updated TS types **N/A** - [x] Updated documentation: **N/A** - [x] Ensured that CI passes **No tests on Windows :'(** - [x] _Tested via patch-packing in an app_
1 parent 3b1d5fc commit 4a48ccf

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

windows/RNScreens/RNScreens.vcxproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
<AppContainerApplication>true</AppContainerApplication>
1414
<ApplicationType>Windows Store</ApplicationType>
1515
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
16-
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
17-
<WindowsTargetPlatformMinVersion>10.0.16299.0</WindowsTargetPlatformMinVersion>
1816
</PropertyGroup>
19-
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2017
<PropertyGroup Label="ReactNativeWindowsProps">
2118
<ReactNativeWindowsDir Condition="'$(ReactNativeWindowsDir)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'node_modules\react-native-windows\package.json'))\node_modules\react-native-windows\</ReactNativeWindowsDir>
2219
</PropertyGroup>
20+
<Import Project="$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props" Condition="Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props')" />
21+
<PropertyGroup Label="Fallback Windows SDK Versions">
22+
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
23+
<WindowsTargetPlatformMinVersion Condition=" '$(WindowsTargetPlatformMinVersion)' == '' ">10.0.16299.0</WindowsTargetPlatformMinVersion>
24+
</PropertyGroup>
25+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2326
<ItemGroup Label="ProjectConfigurations">
2427
<ProjectConfiguration Include="Debug|ARM">
2528
<Configuration>Debug</Configuration>

windows/RNScreens/ScreenStack.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void ScreenStack::removeAllChildren() {
3535

3636
void ScreenStack::removeChildAt(int64_t index) {
3737
m_children.RemoveAt(static_cast<uint32_t>(index));
38+
onChildModified(index);
3839
}
3940

4041
void ScreenStack::replaceChild(
@@ -45,5 +46,20 @@ void ScreenStack::replaceChild(
4546
return;
4647

4748
m_children.SetAt(index, newChild);
49+
onChildModified(index);
50+
}
51+
52+
void ScreenStack::onChildModified(int64_t index) {
53+
// Was it the topmost item in the stack?
54+
if (index >= m_children.Size() - 1) {
55+
if (m_children.Size() == 0) {
56+
// Nobody left
57+
Content(nullptr);
58+
} else {
59+
// Focus on the top item
60+
auto uiElement = m_children.GetAt(m_children.Size() - 1);
61+
Content(uiElement);
62+
}
63+
}
4864
}
4965
} // namespace winrt::RNScreens::implementation

windows/RNScreens/ScreenStack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ScreenStack
1717
m_children;
1818

1919
private:
20+
void onChildModified(int64_t index);
2021
winrt::Microsoft::ReactNative::IReactContext m_reactContext{nullptr};
2122
};
2223
} // namespace winrt::RNScreens::implementation

0 commit comments

Comments
 (0)