如何将字符串转换为React组件

时间:2019-12-14 14:41:01

标签: javascript java android react-native

我有一个文件 myEnternal.js ,我想通过Android中的道具作为组件传递。

myEnternal.js

import React, {Component} from 'react';
import {View} from 'react-native';

export default class ExternalComponent extends Component {
    render() {
        return (
            <View>{ /** add a component a view depending on paremeters passed into class in Props**/}</View>
        );
    }
}  

然后用Java:

Bundle initialProperties = new Bundle();
initialProperties.putString("myEnternal_ComponentString", Files.toString(new File("./myEnternal.js"), Charsets.UTF_8));  

然后可以将其转换为 Component 并添加它。

import React, {Component} from 'react';
import {View} from 'react-native';

export default class HelloWorld extends Component {
    render() {
        var myEnternal_Component = this.props.myEnternal_ComponentString;

        return (
            <View>{ /** convert myEnternal_Component into a component then add <ExternalComponent /> here **/}</View>
        );
    }
}  

如何将组件的字符串表示形式动态转换为组件?

提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

const myEnternal_Component = eval(this.props.myEnternal_ComponentString);

尽管我不建议使用eval