diff --git a/change/react-native-windows-2020-05-21-22-04-55-PR-FixInstanceLoading.json b/change/react-native-windows-2020-05-21-22-04-55-PR-FixInstanceLoading.json new file mode 100644 index 00000000000..3747500288b --- /dev/null +++ b/change/react-native-windows-2020-05-21-22-04-55-PR-FixInstanceLoading.json @@ -0,0 +1,8 @@ +{ + "type": "prerelease", + "comment": "Fix ReactInstance error state to avoid crashes", + "packageName": "react-native-windows", + "email": "vmorozov@microsoft.com", + "dependentChangeType": "patch", + "date": "2020-05-22T05:04:55.630Z" +} diff --git a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp index 151f0141364..4bd4ec76d51 100644 --- a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +++ b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp @@ -407,7 +407,12 @@ void ReactInstanceWin::OnReactInstanceLoaded(const Mso::ErrorCode &errorCode) no if (auto strongThis = weakThis.GetStrongPtr()) { if (!strongThis->m_isLoaded) { strongThis->m_isLoaded = true; - strongThis->m_state = ReactInstanceState::Loaded; + if (!errorCode) { + strongThis->m_state = ReactInstanceState::Loaded; + } else { + strongThis->m_state = ReactInstanceState::HasError; + } + if (auto onLoaded = strongThis->m_options.OnInstanceLoaded.Get()) { onLoaded->Invoke(*strongThis, errorCode); } diff --git a/vnext/Microsoft.ReactNative/RedBox.cpp b/vnext/Microsoft.ReactNative/RedBox.cpp index e7eb49bad95..74e6048b640 100644 --- a/vnext/Microsoft.ReactNative/RedBox.cpp +++ b/vnext/Microsoft.ReactNative/RedBox.cpp @@ -46,10 +46,12 @@ struct RedBox : public std::enable_shared_from_this { } void WindowSizeChanged(winrt::Windows::UI::Core::WindowSizeChangedEventArgs const &args) noexcept { - m_redboxContent.MaxHeight(args.Size().Height); - m_redboxContent.Height(args.Size().Height); - m_redboxContent.MaxWidth(args.Size().Width); - m_redboxContent.Width(args.Size().Width); + if (m_redboxContent) { + m_redboxContent.MaxHeight(args.Size().Height); + m_redboxContent.Height(args.Size().Height); + m_redboxContent.MaxWidth(args.Size().Width); + m_redboxContent.Width(args.Size().Width); + } } void ShowNewJSError() noexcept { @@ -138,6 +140,7 @@ struct RedBox : public std::enable_shared_from_this { m_reloadButton.Click(m_tokenReload); xaml::Window::Current().SizeChanged(m_tokenSizeChanged); m_popup.Closed(m_tokenClosed); + m_redboxContent = nullptr; m_onClosedCallback(GetId()); }