[英]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.