React Native实例

女爷i 2022-09-21 15:29 242阅读 0赞

本文主要包括以下内容

  1. View组件的实例
  2. Text组件实例
  3. Navigator组件实例
  4. TextInput组件实例

View组件的实例

效果如下

这里写图片描述

代码如下

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. class Abc extends Component {
  15. render() {
  16. return (
  17. <View style={styles.flex}>
  18. <View style={styles.container}>
  19. <View style={[styles.item,styles.center]}>
  20. <Text style={styles.font}>酒店</Text>
  21. </View>
  22. <View style={[styles.item,styles.lineLeftRight]}>
  23. <View style={[styles.center,styles.flex,styles.lineCenter]}>
  24. <Text style={styles.font}>海外酒店</Text>
  25. </View>
  26. <View style={[styles.center,styles.flex]}>
  27. <Text style={styles.font}>特惠酒店</Text>
  28. </View>
  29. </View>
  30. <View style={styles.item}>
  31. <View style={[styles.center,styles.flex,styles.lineCenter]}>
  32. <Text style={styles.font}>团购</Text>
  33. </View>
  34. <View style={[styles.center,styles.flex]}>
  35. <Text style={styles.font}>客栈,公寓</Text>
  36. </View>
  37. </View>
  38. </View>
  39. </View>
  40. );
  41. }
  42. }
  43. const styles = StyleSheet.create({
  44. container: {
  45. marginTop:200,
  46. marginLeft:5,
  47. marginRight:5,
  48. height:84,
  49. flexDirection:'row',
  50. borderRadius:5,
  51. padding:2,
  52. backgroundColor:'#FF0067',
  53. },
  54. item: {
  55. flex: 1,
  56. height:80,
  57. },
  58. center:{
  59. justifyContent:'center',
  60. alignItems:'center',
  61. },
  62. flex:{
  63. flex:1,
  64. },
  65. font:{
  66. color:'#fff',
  67. fontSize:16,
  68. fontWeight:'bold',
  69. },
  70. lineLeftRight:{
  71. borderLeftWidth:1/PixelRatio.get(),
  72. borderRightWidth:1/PixelRatio.get(),
  73. borderColor:'#fff',
  74. },
  75. lineCenter:{
  76. borderBottomWidth:1/PixelRatio.get(),
  77. borderColor:'#fff',
  78. },
  79. });
  80. AppRegistry.registerComponent('Abc', () => Abc);

Text组件实例

head.js

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. class Header extends Component {
  15. render() {
  16. return (
  17. <View style={styles.flex}>
  18. <Text style={styles.font}>
  19. <Text style={styles.font_1}>网易</Text>
  20. <Text style={styles.font_2}>新闻</Text>
  21. <Text>有态度"</Text>
  22. </Text>
  23. </View>
  24. );
  25. }
  26. }
  27. const styles = StyleSheet.create({
  28. flex:{
  29. marginTop:25,
  30. height:50,
  31. borderBottomWidth:3/PixelRatio.get(),
  32. borderBottomColor:'#EF2D36',
  33. alignItems:'center',
  34. },
  35. font:{
  36. fontSize:25,
  37. fontWeight:'bold',
  38. textAlign:'center',
  39. },
  40. font_1:{
  41. color:'#CD1D1C',
  42. },
  43. font_2:{
  44. color:'#FFF',
  45. backgroundColor:'#CD1D1C',
  46. },
  47. });
  48. module.exports=Header;

将head.js导入到主JS中的代码为const Header=require(‘./head’);

主JS详细代码

实现了列表,并且有点击效果

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. const Header=require('./head');
  15. class Abc extends Component {
  16. render() {
  17. return (
  18. <View style={styles.flex}>
  19. <Header></Header>
  20. <List title='一线城市楼市退烧 有房源一夜跌价160万'></List>
  21. <List title='上海市民称墓地太贵买不起 买房存骨灰'></List>
  22. <List title='朝鲜再发视频:摧毁青瓦台 一切化作灰烬'></List>
  23. <List title='生活大爆炸人物原型都好牛逼'></List>
  24. <ImportantNews
  25. news={[
  26. '解放军报报社大楼正在拆除 标识已被卸下(图)',
  27. '韩国停签东三省52家旅行社 或为阻止朝旅游创汇',
  28. '南京大学生发起亲吻陌生人活动 有女生献初吻-南京大学生发起亲吻陌生人活动 有女生献初吻-南京大学生发起亲吻陌生人活动 有女生献初吻',
  29. '防总部署长江防汛:以防御98年量级大洪水为目标'
  30. ]}>
  31. </ImportantNews>
  32. </View>
  33. );
  34. }
  35. }
  36. class List extends Component{
  37. render(){
  38. return (
  39. <View style={styles.list_item}>
  40. <Text style={styles.list_item_font}>{
  41. this.props.title}</Text>
  42. </View>
  43. );
  44. }
  45. }
  46. class ImportantNews extends Component{
  47. show(title){
  48. alert(title);
  49. }
  50. render(){
  51. var news=[];
  52. for(var i in this.props.news){
  53. var text=(
  54. <Text
  55. onPress={
  56. this.show.bind(this,this.props.news[i])}
  57. numberOfLines={
  58. 2}
  59. style={styles.news_item}
  60. key={i}
  61. >{
  62. this.props.news[i]}</Text>
  63. );
  64. news.push(text);
  65. }
  66. return (
  67. <View style={styles.flex}>
  68. <Text style={styles.news_title}>今日要闻</Text>
  69. {news}
  70. </View>
  71. );
  72. }
  73. }
  74. const styles = StyleSheet.create({
  75. flex:{
  76. flex:1,
  77. },
  78. list_item:{
  79. height:40,
  80. marginLeft:10,
  81. marginRight:10,
  82. borderBottomWidth:1,
  83. borderBottomColor:'#ddd',
  84. justifyContent:'center',
  85. },
  86. list_item_font:{
  87. fontSize:16,
  88. },
  89. news_item:{
  90. marginLeft:10,
  91. marginRight:10,
  92. fontSize:15,
  93. lineHeight:40,
  94. },
  95. news_title:{
  96. fontSize:20,
  97. fontWeight:'bold',
  98. color:'#CD1D1C',
  99. marginLeft:10,
  100. marginTop:15,
  101. },
  102. });
  103. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

这里写图片描述

这里写图片描述

Navigator组件实例

实现了页面跳转和通过Navigator传递数据并回传数据,在componentDidMount中获取传递过来的数据

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Navigator,
  11. ScrollView,
  12. Text,
  13. PixelRatio,
  14. View
  15. } from 'react-native';
  16. const Header=require('./head');
  17. class Abc extends Component {
  18. render() {
  19. let defaultName='List';
  20. let defaultComponent=List;
  21. return (
  22. <Navigator
  23. initialRoute={
  24. { name: defaultName, component: defaultComponent }}
  25. //配置场景
  26. configureScene=
  27. {
  28. (route) => {
  29. //这个是页面之间跳转时候的动画,具体有哪些?可以看这个目录下,有源代码的: node_modules/react-//native/Libraries/CustomComponents/Navigator/NavigatorSceneConfigs.js
  30. return Navigator.SceneConfigs.VerticalDownSwipeJump;
  31. }
  32. }
  33. renderScene={
  34. (route, navigator) =>
  35. {
  36. let Component = route.component;
  37. return <Component {...route.params} navigator={navigator} />
  38. }
  39. } />
  40. ); }
  41. }
  42. class List extends Component {
  43. constructor(props) {
  44. super(props);
  45. this.state = {
  46. id:1,
  47. user:null,
  48. };
  49. }
  50. _pressButton() {
  51. const { navigator } = this.props;
  52. //为什么这里可以取得 props.navigator?请看上文:
  53. //<Component {...route.params} navigator={navigator} />
  54. //这里传递了navigator作为props
  55. const self=this;
  56. if(navigator) {
  57. navigator.push({
  58. name: 'Detail',
  59. component: Detail,
  60. params:{
  61. id:this.state.id,
  62. //从详情页获取user
  63. getUser: function(user) {
  64. self.setState({
  65. user: user
  66. })
  67. }
  68. }
  69. })
  70. }
  71. }
  72. render(){
  73. if(this.state.user){
  74. return(
  75. <View>
  76. <Text style={styles.list_item}>用户信息: { JSON.stringify(this.state.user) }</Text>
  77. </View>
  78. );
  79. }else{
  80. return (
  81. <ScrollView style={styles.flex}>
  82. <Text style={styles.list_item} onPress={
  83. this._pressButton.bind(this)} >☆ 豪华邮轮济州岛3日游</Text>
  84. <Text style={styles.list_item} onPress={
  85. this._pressButton.bind(this)}>☆ 豪华邮轮台湾3日游</Text>
  86. <Text style={styles.list_item} onPress={
  87. this._pressButton.bind(this)}>☆ 豪华邮轮地中海8日游</Text>
  88. </ScrollView>
  89. );
  90. }
  91. }
  92. }
  93. const USER_MODELS = {
  94. 1: { name: 'mot', age: 23 },
  95. 2: { name: '晴明大大', age: 25 }
  96. };
  97. class Detail extends Component{
  98. constructor(props) {
  99. super(props);
  100. this.state = {
  101. id:null
  102. };
  103. }
  104. componentDidMount() {
  105. //这里获取从FirstPageComponent传递过来的参数: id
  106. this.setState({
  107. id: this.props.id
  108. });
  109. }
  110. _pressButton() {
  111. const { navigator } = this.props;
  112. if(this.props.getUser) {
  113. let user = USER_MODELS[this.props.id];
  114. this.props.getUser(user);
  115. }
  116. if(navigator) {
  117. //很熟悉吧,入栈出栈~ 把当前的页面pop掉,这里就返回到了上一个页面:List了
  118. navigator.pop();
  119. }
  120. }
  121. render(){
  122. return(
  123. <ScrollView>
  124. <Text style={styles.list_item} >传递过来的用户id是:{
  125. this.state.id}</Text>
  126. <Text style={styles.list_item} onPress={
  127. this._pressButton.bind(this)} >点击我可以跳回去</Text>
  128. </ScrollView>
  129. );
  130. }
  131. }
  132. const styles = StyleSheet.create({
  133. flex:{
  134. flex:1,
  135. },
  136. list_item:{
  137. height:40,
  138. marginLeft:10,
  139. marginRight:10,
  140. fontSize:20,
  141. borderBottomWidth:1,
  142. borderBottomColor:'#ddd',
  143. justifyContent:'center',
  144. },
  145. });
  146. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

这里写图片描述

TextInput组件实例

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Navigator,
  11. ScrollView,
  12. TextInput,
  13. Text,
  14. PixelRatio,
  15. View
  16. } from 'react-native';
  17. class Abc extends Component {
  18. render() {
  19. return (
  20. <View style={[styles.flex,styles.topStatus]}>
  21. <Search></Search>
  22. </View>
  23. );
  24. }
  25. }
  26. class Search extends Component {
  27. render(){
  28. return(
  29. <View style={[styles.flex,styles.flexDirection]}>
  30. <View style={[styles.flex,styles.input]}>
  31. <TextInput returnKeyType="search" />
  32. </View>
  33. <View style={styles.btn}>
  34. <Text style={styles.search}>搜索</Text>
  35. </View>
  36. </View>
  37. );
  38. }
  39. }
  40. const styles = StyleSheet.create({
  41. flex:{
  42. flex:1,
  43. },
  44. flexDirection:{
  45. flexDirection:'row',
  46. },
  47. topStatus:{
  48. marginTop:25,
  49. },
  50. input:{
  51. height:45,
  52. borderColor:'red',
  53. borderWidth:1,
  54. marginLeft:10,
  55. paddingLeft:10,
  56. borderRadius:5,
  57. },
  58. btn:{
  59. width:45,
  60. marginLeft:-5,
  61. marginRight:5,
  62. backgroundColor:'#23BEFF',
  63. height:45,
  64. justifyContent:'center',
  65. alignItems:'center',
  66. },
  67. search:{
  68. color:'#fff',
  69. fontSize:15,
  70. fontWeight:'bold',
  71. },
  72. });
  73. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读

    相关 react native

    React Native使你能够在Javascript和[React][]的基础上获得完全一致的开发体验,构建世界一流的原生APP。 React Native

    相关 React Native实例

    本文主要包括以下内容 1. View组件的实例 2. Text组件实例 3. Navigator组件实例 4. TextInput组件实例 View组件的实例