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