2015-12-02 111 views
10

我在React-Native下工作,我正在尋找通過Java傳遞初始道具給JS。這可以容易地在Objective-C與initialProperties做過這樣的:React Native - initialProperties Android

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
                 moduleName:@"myapp" 
               initialProperties:initialProperties 
                launchOptions:launchOptions]; 

凡initialProperties是NSDictionary這將在JSON經由this.props進行轉換,並可用在JS。 所以我期待在Android中也這樣做。任何幫助?謝謝

回答

14

在Android中,您可以將initialPropertieslaunchOptions作爲一個包。

由於在源代碼這裏提到:https://github.com/facebook/react-native/blob/7377fdcc70b25eb023e7c6d1b37eeae2a700cb88/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L325-L334

所以,你可以做這樣的事情:

Bundle initialProps = new Bundle(); 
initialProps.putString("myKey", "myValue"); 

mReactRootView.startReactApplication(mReactInstanceManager, "MyAwesomeApp", initialProps); 
+0

這個奇妙的作品。爲什麼其他答案談論'ReactActivityDelegate'? –

+0

是否可以傳入結構化數據,類似於您可以將值的NSDictionary傳遞給iOS中的React Native?或者只有標量值可能? –

3

由於反應母語v0.20的,你可以覆蓋的getLaunchOptions方法MainActivity.java文件。

@Override 
protected Bundle getLaunchOptions() { 
    Bundle opts = new Bundle(); 
    opts.putBoolean("someValue", true); 
    return opts; 
} 

這將允許您從主應用程序組件的道具訪問someValue

class App extends React.Component { 
    static propTypes = { 
    someValue: PropTypes.bool, 
    }; 

    render() { 
    return <SomeComponent someValue={this.props.someValue} />; 
    } 
} 

AppRegistry.registerComponent('App',() => App); 
3

小心,不贊成這種方式在反應本土> 0.34

https://github.com/facebook/react-native/pull/9320

這是提交信息:

Move `getLaunchOptions` from ReactActivity to ReactActivityDelegate 
Summary: 
After 3c4fd42, `getLaunchOptions` no longer exists in class `ReactActivity`. 
We need refactor UIExplorerActivity to fix the build error. 
Closes #9320 

Differential Revision: D3696381 

Pulled By: astreet 

fbshipit-source-id: 5700cf2363029a95cfbdaf6230e4f82ea69fb472 
master (#2) v0.34.0-rc.0 

因此,如果您更新您的反應版本,您需要更改這部分代碼,並且由於您重寫了父類中不存在的方法,因此您可能不會收到任何錯誤消息, 「會很難調試

+0

儘管此鏈接可能會回答問題,但最好在此處包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/13661616) – Pongpat

+1

對,我編輯了我的答案以顯示提交消息 – Thibaut

8

getlauchOptions已被移動內部ReactActivityDelegate,現在我用這個代碼:

public class MainActivity extends ReactActivity { 

/** 
* Returns the name of the main component registered from JavaScript. 
* This is used to schedule rendering of the component. 
*/ 
@Override 
protected String getMainComponentName() { 
    return "myAppName"; 
} 

@Override 
protected ReactActivityDelegate createReactActivityDelegate() { 
    return new ReactActivityDelegate(this, getMainComponentName()) { 
     @Nullable 
     @Override 
     protected Bundle getLaunchOptions() { 
      Bundle initialProps = new Bundle(); 
      initialProps.putString("SOME_VARIABLE_1", BuildConfig.SOME_VARIABLE_1); 
      initialProps.putString("SOME_VARIABLE_2", "some variable 2 value"); 
      return initialProps; 
     } 
    }; 
} 
+0

非常棒!應該補充說,你需要在頂部添加'import com.facebook.react.ReactActivityDelegate;'並且默認情況下'@ Nullable'在RN中不可用。我只是放棄了它,假設它意味着我只需要總是傳遞一個值。 – ssomnoremac

+0

您可以在回覆中添加@ssomnoremac評論嗎?他們很容易看不見 – Thibaut

3

這裏所有的答案顯得有點過時了。有了React-Native 0.42,這對我有用。

在您的活動(不應用程序)來實現這個目的

@Override 
protected ReactActivityDelegate createReactActivityDelegate() { 
    return new ReactActivityDelegate(this, "My Cool App") { 
     @Nullable 
     @Override 
     protected Bundle getLaunchOptions() { 
      Bundle bundle = new Bundle(); 
      if(MainActivity.this.port != null) { 
       bundle.putInt("port", MainActivity.this.port); 
      } 
      return bundle; 
     } 
    }; 
} 

顯然替換任何你想傳遞給你的主要的道具「端口」響應機組件