【问题标题】:Passing properties from native to React Native when using Wix React Native Navigation (RNN)使用 Wix React Native Navigation (RNN) 时将属性从本机传递到 React Native
【发布时间】:2019-10-17 14:21:45
【问题描述】:

将属性从原生传递到 React Native 描述为 here。总结如下:

NSArray *imageList = @[@"http://foo.com/bar1.png",
                       @"http://foo.com/bar2.png"];

NSDictionary *props = @{@"images" : imageList};

RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                 moduleName:@"ImageBrowserApp"
                                          initialProperties:props];

但是,当使用Wix React Native Navigation 时,没有rootView。根据文档,这是 iOS 上最小的 RNN 设置:

 #import "AppDelegate.h"

 #import <React/RCTBundleURLProvider.h>
 #import <React/RCTRootView.h>
 #import <ReactNativeNavigation/ReactNativeNavigation.h>

 @implementation AppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
     [ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];

     return YES;
 }

 @end

我们如何从原生传递道具?

【问题讨论】:

    标签: react-native react-native-navigation wix-react-native-navigation


    【解决方案1】:

    为此,您需要create a native module。该模块公开了通过resolving a promise返回结果的公共方法。

    【讨论】:

      【解决方案2】:

      当我将属性从 React Native 传递到原生 iOS 时,您需要检查原生 iOS 到 React Native 的属性传递。这是我的代码

      在 iOS 中

      Appdelegate.h

      #import <UIKit/UIKit.h>
      #import "React/RCTBridgeModule.h"
      
      @import Firebase;
      @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeModule>
      
      @property (strong, nonatomic) UIWindow *window;
      
      
      @end
      

      Appdelegate.m

      #import "RCTBundleURLProvider.h"
      #import <React/RCTLog.h>
      
      @implementation AppDelegate
      
      RCT_EXPORT_MODULE();
      RCT_EXPORT_METHOD(addEvent:(NSString *)propertyName)
      {
          //Here you get your property value
          var property = propertyName;
          RCTLogInfo(@"Pretending to create an event %@ at %@", areaId);
      }
      

      在 React Native 中

      MyComponent.js

      import { NativeModules } from 'react-native';
      
      var AppDelegate = NativeModules.AppDelegate;
      AppDelegate.addEvent(value);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 2018-12-03
        • 2018-10-29
        相关资源
        最近更新 更多