簡體   English   中英

React Native模塊中的依賴注入

[英]Dependency Injection in React Native modules

我們正在逐步將我的應用程序轉換為React Native。 我一直在iOS上的React Native中遇到依賴注入問題。

我在我的應用程序中有一些服務,我想在本機模塊中使用。 目前,它們是通過台風注入的,一切正常。

但是,react native本身會將任何本機模塊初始化並維護為單例。 這使我無法讓Typhoon初始化它們,因此我無法將依賴關系注入其中。

可以做些什么? 自己創建RCTBridge是一個選項,但感覺非常低級,並且仍然需要弄清楚如何將其注入到UIView中。

我不確定為什么你的問題沒有獲得更多的選票; 我自己也在努力回答同樣的問題,並認為我應該繼續回答我的第一個StackOverflow問題!

圍繞RCTBridge類進行挖掘提供了答案。 您需要做的是使用實現RCTBridgeProtocol的類的實例手動初始化RCTBridge(重要的是方法'extraModulesForBridge';如果您願意,甚至可以在View Controller中實現此協議。

// Initialise a class that implements RCTBridgeDelegate
// Be warned, RCTBridge won't store a strong reference to your delegate.
// You should there store this delegate as a property of your initialising class, or implement the protocol in the View Controller itself. 
id<RCTBridgeDelegate> moduleInitialiser = [[classThatImplementsRCTBridgeDelegate alloc] init];

// Create a RCTBridge instance 
// (you may store this in a static context if you wish to use with other RCTViews you might initialise.  
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:nil];

// Initialise an RCTRootView
RCTRootView *rootView = [[RCTRootView alloc]
                     initWithBridge:bridge
                         moduleName:kModuleName
                  initialProperties:nil];

// Note that your class that implements RCTBridgeDelegate SHOULD implement the following methods and it might look something like this.

// This will return the location of our Bundle
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
    return [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"];
}

// Importantly, this is the method we need to implement to answer this question
// We must return an array of initialised Modules
// Be warned, I am writing this off of the top of my head, so excuse any syntax errors there might be!
- (NSArray *)extraModulesForBridge:(RCTBridge *)bridge
{
   return @[[OurNativeModule alloc] initWithCustomInitialiser:customDependency]];
}

編輯:我將此添加到React-native文檔中。 https://facebook.github.io/react-native/docs/native-modules-ios.html#dependency-injection

上面的代碼工作得很好。 這是代碼的Swift 4版本。

@objc(RNModuleInitialiser)
final class RNModuleInitialiser: NSObject {

    //Inject your dependencies here
    init() {

    }

}

extension RNModuleInitialiser: RCTBridgeDelegate {

    func sourceURL(for bridge: RCTBridge!) -> URL! {
        return URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")!
    }

    func extraModules(for bridge: RCTBridge!) -> [RCTBridgeModule]! {

        var extraModules = [RCTBridgeModule]()

        //Initialise the modules here using the dependencies injected above

        return extraModules
    }

}

初始化橋時,傳遞moduleInitialiser:

//Make sure to hold the strong reference to the moduleInitaliser

self.moduleInitialiser = RNModuleInitialiser()
self.bridge = RCTBridge(delegate: self.moduleInitialiser, launchOptions: nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM