如何从本地android传递数据以响应本地? [英] How to pass data from native android to react native?

查看:53
本文介绍了如何从本地android传递数据以响应本地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从本地android Main Activity类传递一个字符串值来响应本地组件.我应用了此代码,但是一旦我在日志中打印数据",它就会显示未定义.我无法理解问题所在.我是React Native的初学者.预先感谢.

I want to pass one string value from native android Main Activity class to react native component. I apply this code, but once I print the "data" in the log it shows undefined. I am not able to understand what is the problem. I am a beginner in react native. Thanks in advance.

在主要活动中:

@Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {
            @Override
            protected Bundle getLaunchOptions() {

                Bundle initialProperties = new Bundle();
                initialProperties.putString("var_1","Im the first var");
                return initialProperties;
            }
        };
    }

在App.js中:

export default class App extends Component<Props> {

  render() {
    var data = this.props.var_1;
    console.log(data);
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.welcome}>{this.props.var_1}</Text> 
      </View>
    );
  }
}

推荐答案

有几种方法:

您可以通过从android活动中发出事件来发送它,并在您的react组件中处理该事件.

You can send it by emitting event from android activity and handle that event in your react component.

    //Emitting the event from Android Activity.

    public class MainActivity extends ReactActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            WritableMap map = Arguments.createMap();
            map.putString("key1", "Value1");
            map.putString("key1", "Value1");

        try {
           getReactInstanceManager().getCurrentReactContext()   
          .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
          .emit("customEventName", map);

            } catch (Exception e){
              Log.e("ReactNative", "Caught Exception: " + e.getMessage());
            }
        }

然后在react组件中,addEventListner(subscribe)对该事件进行如下处理:

And in react component addEventListner(subscribe) to that event like as:

import { DeviceEventEmitter } from 'react-native';

componentWillMount: function() {
  DeviceEventEmitter.addListener('customEventName', function(e: Event) {
    // handle event and you will get a value in event object, you can log it here
  });
}


另一种方法是:


Another way is:

您可以将该字符串存储在SharedPreferences中,并可以从React-native组件访问相同的值.有关共享首选项的使用,请参见下面的链接: https://github.com/sriraman/react-native-shared-preferences

You can store that string in SharedPreferences and can access the same value from the React-native component. Refer below link for the shared preferences use: https://github.com/sriraman/react-native-shared-preferences

这篇关于如何从本地android传递数据以响应本地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆