Skip to content

Can RCTViewComponentView get access RCTBridge's bundleURL or launchOptions property? #51010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nanatlantica opened this issue Apr 30, 2025 · 10 comments
Labels
📦Bundler Needs: Triage 🔍 Newer Patch Available Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@nanatlantica
Copy link

Description

In Legacy architecture, developer can access RCTBridge instance in RCTViewManager to get access to the RCTBridge's bundleURL or launchOptions property
In Fabric Component,how could I access the bundleURL or launchOptions property in RCTBridge?

Steps to reproduce

1.Create an instance inherited from RCTViewComponentView
2.How can I get access to bridge.bundleURL and launchOptions?

React Native Version

0.77.0

Affected Platforms

Runtime - iOS

Areas

Fabric - The New Renderer

Output of npx @react-native-community/cli info

System:
  OS: macOS 14.2.1
  CPU: (8) arm64 Apple M1 Pro
  Memory: 76.88 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.5.1
    path: /opt/homebrew/bin/node
  Yarn:
    version: 1.22.21
    path: /opt/homebrew/bin/yarn
  npm:
    version: 9.8.0
    path: /opt/homebrew/bin/npm
  Watchman:
    version: 2024.01.15.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.11.3
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: Not Found
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 20.0.1
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.77.0
    wanted: 0.77.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: false
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

-

MANDATORY Reproducer

https://github.com/nanatlantica/RNDynamic

Screenshots and Videos

No response

@nanatlantica nanatlantica added Needs: Triage 🔍 Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) labels Apr 30, 2025
@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - 0.77.2. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@cipolleschi
Copy link
Contributor

Hi @nanatlantica, thanks for the issue.

The New Architecture is more opinionated on how to handle lifecycle and on how to let components and modules communicate.
We want to keep the two concepts as separated as possible. There should be no need for a component to access the bundle URL or the launch options in the New Architecture. So, we didn't designed any API to do so.

Can you explain what is your use case, and what you are trying to achieve, so that we can suggest an alternative?

@nanatlantica
Copy link
Author

nanatlantica commented May 1, 2025

Hi @cipolleschi ,Thanks for reply.

For example, here is my code:

  • (AHNavigationController *)getCurrentNavigationController

{
AHNavigationController * nav = [_bridge.launchOptions objectForKey:@"autohome_rnbrowser_nav_private"];
return nav;
}

[[self getCurrentNavigationController] pushViewController:vc animatiomType:AHNavigationViewAnimationTypeBottomToTop];

I create a RCTBridge instance with specificed launchOptions : an UIViewController instance,then I use this UIViewController instance to call push or pop function in legacy component.
How can I implement this functionality within the Fabric component?

@cipolleschi
Copy link
Contributor

If that's your use case, I don't think you should use any actual React Native specific detail.
You know the ViewController at the time that React Native is created and launched. Why not saving it in a static variable or in a singleton that keeps the navigation controller details, instead?

Also, is your Fabric component a navigation primitive? Are you creating a navigation library?

@nanatlantica
Copy link
Author

@cipolleschi Yes, this is one type of scenario. I also have several other scenarios here. For example: this control needs to report some data to the server. These data are placed in the launchOptions when initializing the RCTBridge. How can I obtain this data that needs to be reported?

@nanatlantica
Copy link
Author

@cipolleschi ,any suggestions for my question?

@cipolleschi
Copy link
Contributor

cipolleschi commented May 8, 2025

I believe your app is putting those data in the launchOptions, am I correct? Instead of storing them in the launchOptions, you can create a simple singleton object that holds those data, and that can be accessed by the app wherever you need.

// SingletonDataStore.h
#import <Foundation/Foundation.h>
@interface SingletonDataStore : NSObject
+ (instancetype)sharedInstance;
- (void)setData:(id)data forKey:(NSString *)key;
- (id)dataForKey:(NSString *)key;
@end
// SingletonDataStore.m
#import "SingletonDataStore.h"
@implementation SingletonDataStore
{
    NSMutableDictionary *_dataStore;
}
+ (instancetype)sharedInstance
{
    static SingletonDataStore *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _dataStore = [[NSMutableDictionary alloc] init];
    }
    return self;
}
- (void)setData:(id)data forKey:(NSString *)key
{
    [_dataStore setObject:data forKey:key];
}
- (id)dataForKey:(NSString *)key
{
    return [_dataStore objectForKey:key];
}
@end

And you can use it like this:

// In the app delegate
#import "SingletonDataStore.h"
// Store data
[[SingletonDataStore sharedInstance] setData:@"Hello, World!" forKey:@"greeting"];
// in the component
#import "SingletonDataStore.h"

// Retrieve data
NSString *greeting = [[SingletonDataStore sharedInstance] dataForKey:@"greeting"];

or you can use UserDefaults

@nanatlantica
Copy link
Author

@cipolleschi Sorry, this class doesn't meet my needs. If my app has multiple RCTBridge instances simultaneously, how can my component distinguish which RCTBridge's launchOptions to use during initialization?

@cipolleschi
Copy link
Contributor

There is no bridge in the New Architecture. You should not rely on the bridge as we are migrating away from it.
Also, any component in the old architecture always receive one bridge. There is on way to get different RCTBridges instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦Bundler Needs: Triage 🔍 Newer Patch Available Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
Development

No branches or pull requests

3 participants