2015-12-02 34 views
10

私はReact-Nativeの下で働いています。私はJavaを介してJSに初期小道具を渡しています。これは、このようなinitialPropertiesとObjective-Cで容易に行うことができる。React Native - initialProperties Android

initialPropertiesは this.props介してJSにJSONに変換して利用できるようになり NSDictionaryある
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
                 moduleName:@"myapp" 
               initialProperties:initialProperties 
                launchOptions:launchOptions]; 

。 私はAndroidでも同じことをやろうとしています。どんな助け?ありがとう

答えて

14

Androidでは、launchOptionsをバンドルとしてinitialPropertiesに渡すことができます。ソースコードにここで言及されたよう

https://github.com/facebook/react-native/blob/7377fdcc70b25eb023e7c6d1b37eeae2a700cb88/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L325-L334

ですから、このような何か行うことができます反応するネイティブv0.20のとおり

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

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

反応し、あなたのメインの小道具に渡したいものは何でもして「ポート」を置き換えません。他の答えはなぜ「ReactActivityDelegate」について話していますか? –

+0

NSDictionaryの値をiOSのReact Nativeに渡す方法と同様に、構造化データを渡すことは可能ですか?スカラー値のみが可能ですか? –

3

を、あなたはで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 

したがって、あなたのバージョンのreactを更新する場合は、コードのこの部分を変更する必要があります。また、親クラスに存在しないメソッドをオーバーライドしているため、エラーメッセージが表示されず、 「今、私はこのコードを使用し、ReactActivityDelegateの内側に移動された

+0

このリンクは質問に答えるかもしれませんが、回答の重要な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューから](レビュー/低品質の投稿/ 13661616) – Pongpat

+1

右、コミットメッセージを表示するために私の答えを編集 – Thibaut

8

getlauchOptionsをデバッグするのは難しいことでしょう:

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; 
     } 
    }; 
} 

は明らかにこれは素晴らしい作品ネイティブコンポーネント

関連する問題