Skip to content

Commit

Permalink
fix(iOS) [0.74]: RCTRedBox not appearing in Bridgeless when metro is …
Browse files Browse the repository at this point in the history
…not running (#43147)

Summary:
When testing out `0.74.0-rc0` I found that when the metro is not running we are not displaying RedBox which bumps users to start the packager and reload the app. It also fixes the case where users try to reload by clicking the "Reload" button on RedBox.

## Before

https://github.com/facebook/react-native/assets/52801365/086c557f-ea1f-4a97-b4c7-df8a945cc7a0

## After

https://github.com/facebook/react-native/assets/52801365/9f8421b3-5e83-466f-8cdb-38f97981275d

## Changelog:

[IOS] [FIXED] - RCTRedBox not appearing in Bridgeless when metro is not running

Pull Request resolved: #43147

Test Plan: Build the app without metro running check if RedBox is shown

Reviewed By: javache

Differential Revision: D54632056

Pulled By: dmytrorykun

fbshipit-source-id: fb6742898d3bd82545bfffd9175208e1a5984cb6
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Mar 8, 2024
1 parent 642b4e5 commit 4adef35
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/react-native/React/CoreModules/RCTRedBox.mm
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ - (void)dismiss

- (void)reload
{
[_actionDelegate reloadFromRedBoxController:self];
if (_actionDelegate != nil) {
[_actionDelegate reloadFromRedBoxController:self];
} else {
// In bridgeless mode `RCTRedBox` gets deallocated, we need to notify listeners anyway.
RCTTriggerReloadCommandListeners(@"Redbox");
[self dismiss];
}
}

- (void)showExtraDataViewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#import <React/RCTLogBox.h>
#import <React/RCTModuleData.h>
#import <React/RCTPerformanceLogger.h>
#import <React/RCTRedBox.h>
#import <React/RCTReloadCommand.h>
#import <React/RCTSurfacePresenter.h>
#import <ReactCommon/RCTTurboModuleManager.h>
#import <ReactCommon/RuntimeExecutor.h>
Expand Down Expand Up @@ -122,10 +124,17 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
}];
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];

[defaultCenter addObserver:self
selector:@selector(didReceiveReloadCommand)
name:RCTTriggerReloadCommandNotification
object:nil];

[self _start];
}
Expand Down Expand Up @@ -389,6 +398,24 @@ - (void)_attachBridgelessAPIsToModule:(id<RCTTurboModule>)module
}
}

- (void)handleBundleLoadingError:(NSError *)error
{
if (!_valid) {
return;
}

RCTRedBox *redBox = [_turboModuleManager moduleForName:"RedBox"];

RCTExecuteOnMainQueue(^{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidFailToLoadNotification
object:self
userInfo:@{@"error" : error}];
[redBox showErrorMessage:[error localizedDescription]];

RCTFatal(error);
});
}

- (void)_loadJSBundle:(NSURL *)sourceURL
{
#if RCT_DEV_MENU && __has_include(<React/RCTDevLoadingViewProtocol.h>)
Expand Down Expand Up @@ -420,8 +447,7 @@ - (void)_loadJSBundle:(NSURL *)sourceURL
}

if (error) {
// TODO(T91461138): Properly address bundle loading errors.
RCTLogError(@"RCTInstance: Error while loading bundle: %@", error);
[strongSelf handleBundleLoadingError:error];
[strongSelf invalidate];
return;
}
Expand Down Expand Up @@ -490,4 +516,9 @@ - (void)_handleJSErrorMap:(facebook::react::MapBuffer)errorMap
isFatal:errorMap.getBool(JSErrorHandlerKey::kIsFatal)];
}

- (void)didReceiveReloadCommand
{
[self _loadJSBundle:[_bridgeModuleDecorator.bundleManager bundleURL]];
}

@end

0 comments on commit 4adef35

Please sign in to comment.