我看到很多人在React Native中创建类似于下面的路由映射:
if (route.id === 'Blah') {
return (<Blah prop1={this.method} prop2={this.other method} />);
} else if (route.id === 'OtherView') {
return (<OtherView prop1={this.method} />);
}
这段代码很容易变得非常冗长,我希望能像这样做:
return (React.createElement(route.id, {propsToPass}));
在React Native中,似乎“字符串不能作为React Native中的第一个参数,因为这些字符串应该用于常规React中的html标签。”因此,这种方法无法使用。
那么如何解决?如果我提供ReactClass作为第一个参数或使用eval(route.id)(但我知道这可能是危险的),我可以使其正常工作。
我该如何使用字符串创建一个React Native元素呢?