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

标签 javascript java android react-native

我有一个文件 myEnternal.js,我想将其作为组件通过 Android 的 Props 传递。

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

然后可能将其转换为组件并添加它。

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

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

提前谢谢大家。

最佳答案

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

虽然我不建议使用eval

关于javascript - 如何将字符串转换为 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59336111/

相关文章:

java - 本地 Tomcat 环境未启动

java - 使用泛型时使用什么规则进行类型推断

android - FTS 相对于定制解决方案的优势是什么?

android - 理解 Dagger 2 @Component.Builder 注解

javascript - 谷歌地图 API : How to add a marker and speech bubble?

javascript - Highcharts - 图表外的图例定位

javascript - 数据库没有更新

Java:设计问题 - 集合之间的最小对

java - Android Dagger 2 不在 java/kotlin 项目中生成组件

javascript - 我如何将 For 循环与数组一起使用并通过单击一个一个地获取值