繁体   English   中英

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

[英]How to convert a String into a React Component

我有一个文件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));  

然后可能将其转换为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>
        );
    }
}  

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

谢谢大家。

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

虽然我不建议使用eval

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM