如何在不使用JSX的情况下在React Native中制作组件
How do I make components in React Native without using JSX?
使用React Native的规范方法显然是使用JSX:
render: function() {
var movie = MOCKED_MOVIES_DATA[0];
return (
<View style={styles.container}>
<Text>{movie.title}</Text>
<Text>{movie.year}</Text>
<Image source={{uri: movie.posters.thumbnail}} />
</View>
);
}
我该如何仅使用JavaScript来完成此操作?通常在正常的React中,React.createElement('div')
可以作为JSX的替代品,但在尝试运行我的iOS React Native应用程序时,我收到了一个错误"null不是函数"。
我尝试联系打包程序,它说它在8081端口上侦听,还说它在运行时收到index.ios.bundle
的请求。
所以我把这个放在浏览器中:http://localhost:8081/index.ios.bundle
在返回的包裹中,我发现:
var wazoo = React.createClass({displayName: "wazoo",
render: function() {
return (
React.createElement(View, {style: styles.container},
React.createElement(ScrollView, null,
React.createElement(View, null,
React.createElement(Text, {style: styles.welcome},
"Wazoo"
),
等等。所以看起来View
、ScrollView
等就像Web React中通常定义的组件一样,上面的代码就是相当于JSX的JS。
Daniel Earwicker的解决方案是正确的,但我们可以使用工厂使其更可读:
var View = React.createFactory(React.View);
var ScrollView = React.createFactory(React.ScrollView);
var Text = React.createFactory(React.Text);
var wazoo = React.createClass({displayName: "wazoo",
render: function() {
return View({style: styles.container},
ScrollView(null,
View(null,
Text({style: styles.welcome},
"Wazoo"
)
)
)
);
}
});
我知道最初的问题已经有一段时间了,但如果其他人感兴趣,你可以查看我们在Uqbar:上制作的库
https://www.npmjs.com/package/njsx
它非常易于使用,并且提供了比React开箱即用界面更干净的界面。
相关文章:
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- 使用Jest测试React Native应用程序
- 是否存在React Native“;WEB代码安全防护”;
- React Native Listview留有余地
- 如何在React Native中绘制图形
- 在iOS和Android上使用React native时,我还能使用本机第三方lirbraries吗
- 使用createContainer将Meteor数据获取到React Native中时出现问题
- React Native当前是否支持访问用户'的通讯簿
- React Native-What'It’在任何地方都要留下逗号
- React Native DeviceEventEmitter键盘WillShow停止工作
- 与React Native的Chart.js等效
- 如何使用webview在React Native中使用Highcharts
- 如何在React Native的MapView中设置标记
- React Native NavigatorIOS
- 如何获得React Native TextInput以在提交后保持焦点
- 使用react native检测应用程序终止
- 可以在React Native中制作一个自哈希应用程序
- React Native + React - Native -router-flux:
- React Native + React - Native -router-flux:如何仅将hideNavBar应用于
- React Native-React Packager发送旧代码