先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=1277855849978417&vid=s14139cbg2q
我看了一下,挺全的,我接下来应该会根据这个视频自己做一个App?
这个链接也是我在GitHub上面学习各位可爱的程序员的开源项目的时候看到的,接下来我们先来看这个博主的项目吧~
先放github地址:https://github.com/linchengzzz/rnTest
接下来我们来分析项目
项目运行出来的效果,应该是接口有些问题



接下来我们简单看看代码,发现亮点

//index.js
//这个是根入口文件
/*** @format* @lint-ignore-every XPLATJSCOPYRIGHT1*/import {AppRegistry} from 'react-native';
// import App from './App';
import App from './views/main.js';
import {name as appName} from './app.json';AppRegistry.registerComponent(appName, () => App);

我们接下来看main.js

//views/main.js
import React, { Component } from 'react';
import { Icon } from 'react-native-elements';
//引入页面
import AboutPage from './book/about';
import BookPage from './book/index';
import MoviePage from './movie/index';
import BookDetailsPage from './book/detail';
import MovieDetailsPage from './common/webpage';
import { createStackNavigator, createBottomTabNavigator, createAppContainer } from "react-navigation";
//定义切换页面
const bottomTabNavigator = createBottomTabNavigator({Book: BookPage,Movie: MoviePage,About: AboutPage}, {defaultNavigationOptions: ({ navigation }) => ({tabBarIcon: ({ focused, tintColor }) => {const { routeName } = navigation.state;let iconName;if (routeName === 'Book') {iconName = focused ? 'book' : 'book' ; //可以根据focused更换图标} else if (routeName === 'Movie') {iconName = 'movie';}else{iconName = 'stars';}return <Icon name={iconName} size={25} color={tintColor} />;},}),//定义激活颜色,有的是直接改变图片tabBarOptions: {//根据是否激活,改变颜色activeTintColor: 'tomato', inactiveTintColor: 'gray',}});// 定义对应页面
const AppStack = createStackNavigator({bottomTabNavigator: {screen: bottomTabNavigator},Details: {screen: BookDetailsPage,// navigationOptions: {//     title: "图书详情"// }},MovieDetails:{screen: MovieDetailsPage,}}, {initialRouteName: "bottomTabNavigator",// 默认header bar样式配置defaultNavigationOptions: {headerTintColor: '#fff',headerStyle: {backgroundColor: '#2596f3',height: 0 //影藏header}},});
const AppContainer = createAppContainer(AppStack);export default class App extends Component {render() {return (<AppContainer />);}
}

分析对应页面

//views/book/about.js
import React, { Component } from 'react';
import { Text, View, ScrollView, StyleSheet } from 'react-native';
export default class About extends Component {render(){return (<ScrollView><View style={styles.container}><Text style={styles.title}>App Info</Text><View style={styles.box}><Text style={styles.leftTitle}>版本</Text><Text style={styles.rightContent}>v1.1_20190312</Text></View><View style={styles.box}><Text style={styles.leftTitle}>课程地址</Text><Text style={styles.rightContent}>https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=1278010468801073&vid=e1414ek8gbc</Text></View><View style={styles.box}><Text style={styles.leftTitle}>依赖组件</Text><Text style={styles.rightContent}>react-native-elements、react-navigation、react-native-webview</Text></View><View style={styles.box}><Text style={styles.leftTitle}>欠缺功能</Text><Text style={styles.rightContent}>header:目前影藏了,设置了header高度为0</Text></View>             </View></ScrollView>)}
}
var styles = StyleSheet.create({container: {flex: 1,padding: 10,marginTop: 0,lineHeight:30},title: {fontWeight: "bold",color: "#f33",fontSize: 20},box:{marginTop:20,flex:1},leftTitle:{fontSize:18,color:"#333",fontWeight:"800",},rightContent:{fontSize:16,color:"#999"}
})
//封装的searchBar
//views/common/searchbar.js
import React, { Component } from 'react';
import { Text, View,TextInput, StyleSheet, TouchableOpacity } from 'react-native';export default class SearchBar extends Component {render() {return (<View style={styles.container}><View style={styles.inputContainer}><TextInput style={styles.input} {...this.props} /></View><TouchableOpacity style={styles.btn} {...this.props}><Text style={styles.search}>搜索</Text></TouchableOpacity></View>);}
}var styles = StyleSheet.create({container:{flexDirection:"row",justifyContent:"flex-end",alignItems:"center",height:44,marginTop:10},inputContainer:{flex:1,marginLeft:5},input:{flex:1,height:44,borderWidth:1,borderRadius:4,borderColor:"#ccc",paddingLeft:5},btn:{width:55,height:44,marginLeft:5,marginRight:5,backgroundColor:"#23beff",borderRadius:4,justifyContent:"center",alignItems:"center"},search:{flex:1,color:"#fff",fontSize:15,fontWeight:"bold",textAlign:"center",lineHeight:44}
})
//views/book/index.js
import React, { Component } from 'react';
import { Text, View, ScrollView, Image, StyleSheet,TouchableOpacity } from 'react-native';
import SearchBar from '../common/searchbar';
import Util from '../common/util';
import Api from '../common/api';
export default class index extends Component {constructor(props) {super(props);this.state = {data: [],show: true,keyword: 'react'};}componentDidMount(){// 初次请求数据this.getData();}updateSearch = search => {this.setState({ keyword: search });   }//获取数据searchText=()=>{this.getData();}// 以下写法报错,不识别this// searchText (){//     this.getData();// }// Util.loading 工具函数定义的loadinggetData(){// 显示loadingthis.setState({show: false});// 请求数据var that = this;var url = Api.book_search + '?count=20&q=' + this.state.keyword;Util.getRequest(url, function (response) {// 请求成功if (!response.books || response.books.length == 0) {return alert("未查询到数据");}// 显示loading,将请求结果赋值给datathat.setState({show: true,data: response.books});}, function (error) {// 请求失败alert(error);});}render() {return (<ScrollView>{/* 封装的搜索头部 */}<SearchBarplaceholder="请输入关键词(书名、作者)..."onChangeText={this.updateSearch}onPress={this.searchText}/>{// 请求数据时显示loading,请求成功显示列表this.state.show ?<View style={styles.container} >{this.state.data.map((item, i) => {return (<TouchableOpacity style={styles.list} key={i} onPress={() => this.props.navigation.push('Details', { 'bookID': item.id })}activeOpacity={0.5}><Image source={{ uri: item.images.small }} style={styles.images} /><View style={styles.rightbox}><Text style={styles.title}>{item.title}</Text><Text>价格:{item.price ? item.price : '暂无'}</Text><Text>作者:{item.author.map(function(vo){return vo + ' ';})}</Text><Text>{item.publisher} {item.pubdate}</Text><Text>{item.pages ? item.pages : '未知'} 页</Text></View></TouchableOpacity>);})}</View>                    : Util.loading}</ScrollView>)}
}var styles = StyleSheet.create({container: {flex: 1,alignItems: 'center',justifyContent: "center",padding:10,marginTop: 0},btn:{width:100,height:30},images: { width: 80, height: 100 },title:{fontWeight:"bold",color:"#f33"},rightbox:{flex:1,marginLeft:10},list:{flex: 1,flexDirection: "row",borderBottomColor: "#ccc",borderBottomWidth: 1,paddingTop:10,paddingBottom:10}
})

关于详情页

//views/book/detail.js
import React, { Component } from 'react';
import { Text, View, ScrollView, Image, StyleSheet } from 'react-native';
import { Icon,Header,Button } from 'react-native-elements';
import Util from '../common/util';
import Api from '../common/api';
import { TouchableHighlight, TouchableOpacity } from 'react-native-gesture-handler';
export default class BookDetail extends Component {constructor(props) {super(props);this.state = {bookID: '',bookData: null};}componentDidMount(){// bookID = this.props.navigation.getParam('bookID', 26378583);// this.setState({//     bookID: bookID// })this.getData();}getData(){//这个是从后端获取的数据// var url = Api.book_detail_id + this.state.bookID;var url = Api.book_detail_id + this.props.navigation.getParam('bookID', 26378583);var that = this;Util.getRequest(url,function(data){that.setState({bookData: data})},function(error){alert(error);})}render(){var bookData = this.state.bookData;return (<ScrollView>{bookData != null ?<View>{/* <Buttonicon={{name: "assignment-return",size: 15,color: "white"}}onPress={() => this.props.navigation.goBack()}title="返回"/>   */}<View style={styles.list}><Image source={{ uri: bookData.images.small }} style={styles.images} /><View style={styles.rightbox}><Text style={styles.title}>{bookData.title}</Text><Text>价格:{bookData.price ? bookData.price : '暂无'}</Text><Text>作者:{bookData.author.map(function (vo) {return vo + ' ';})}</Text><Text>{bookData.publisher} {bookData.pubdate}</Text><Text>{bookData.pages ? bookData.pages : '未知'} 页</Text></View></View><View style={{ marginTop: 10 }}><Text>图书简介</Text><Text>{bookData.summary}</Text></View><View style={{marginTop:10}}><Text>作者简介</Text><Text>{bookData.author_intro}</Text></View></View>: Util.loading}</ScrollView>)}}
var styles = StyleSheet.create({container: {flex: 1,alignItems: 'center',justifyContent: "center",padding: 10,marginTop: 0},btn: {width: 100,height: 30},images: { width: 80, height: 100 },title: {fontWeight: "bold",color: "#f33"},rightbox: {flex: 1,marginLeft: 10},list: {flex: 1,flexDirection: "row",borderBottomColor: "#ccc",borderBottomWidth: 1,paddingTop: 10,paddingBottom: 10}
})

转载于:https://www.cnblogs.com/smart-girl/p/10917986.html

【水滴石穿】react-native-book相关推荐

  1. 基于 React Native 的 58 同城 App 开发实践

    作者简介: 彭飞,58 同城 iOS 客户端架构师.专注于新技术的研发,主要负责 App 端组件化架构以及性能优化,并已推广 React Native 在 58 同城 App 中业务场景的应用.在 M ...

  2. fragment中文网_更新 · React Native 中文网

    时刻将 React Native 更新到最新的版本,可以获得更多 API.视图.开发者工具以及其他一些好东西(译注:官方开发任务繁重,人手紧缺,几乎不会对旧版本提供维护支持,所以即便更新可能带来一些兼 ...

  3. React Native —— App

    使用 React Native 作为 app 框架,Redux 作为数据层,因此我们需要选用一些支持性的技术和工具: 开源的 Parse Server 做数据存储 - 运行在 Node.js 上. F ...

  4. 【React Native】iOS原生导航跳转RN页面

    上一篇介绍了React Native使用react-navigation进行导航跳转页面,现在我们介绍下原生iOS中怎么导航进一个新的React Native页面. 一.原生跳转React Nativ ...

  5. 【React Native】react-navigation导航使用方法

    目录 集成react-navigation 使用react-navigation 上一篇介绍了如何在已有iOS项目中集成React Native.这一篇我们把上一篇的demo做下拓展,添加点击电影跳转 ...

  6. 给iOS开发者的React Native入门使用教程

    目录 一. 原生iOS项目集成React Native 二. 原生跳转RN页面 三. 显示豆瓣热门电影列表 四. 改为导航 五.完整源代码 一. 原生iOS项目集成React Native 创建一个新 ...

  7. 对比React Native、dcloud、LuaView三个框架技术(内部)

    转载自:http://www.jianshu.com/p/ee1cdb33db8d 主要对比React Native和5+SDK(就是dcloud的SDK)两个: 开发语言:三个都是用其他语言来统一开 ...

  8. 初步了解React Native的新组件库firstBorn

    first-born is a React Native UI Component Framework, which follows the design methodology Atomic Des ...

  9. 我在React Native中构建时获得的经验教训

    by Amanda Bullington 通过阿曼达·布林顿(Amanda Bullington) 我在React Native中构建时获得的经验教训 (Lessons I learned while ...

  10. React Native开源项目如何运行(附一波开源项目)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

最新文章

  1. 改变myeclipse北京颜色
  2. 微软代号为“Volta”的编程工具集预览
  3. 【转】NHIBERNATE的各种保存方式的区别 (SAVE,PERSIST,UPDATE,SAVEORUPDTE,MERGE,FLUSH,LOCK)
  4. android布局的一些知识
  5. android studio python_android studio的安装信息
  6. Java的代码书写规范
  7. 派生类对基类成员的访问控制之公有继承
  8. 【原】浅谈KL散度(相对熵)在用户画像中的应用
  9. 求证:为什么当x趋近于0时,(sinx)/x的极限等于1
  10. mapxtreme概述
  11. 注册表禁用计算机管理,注册表被管理员禁用如何处理 怎么解决注册表被管理员禁用【图文】...
  12. 读书百客:《宴清都·初春》赏析
  13. 怎样批量查询网站是否被搜狗收录?批量查询网站搜狗收录的详细教程
  14. python数据结构之单链表
  15. AlexNet模型及代码详解
  16. 强大、优秀的文件管理软件评测:图片管理、书籍管理、文献管理
  17. 第14章 数据分析案例--Python for Data Analysis 2nd
  18. 关于outlook无法打开,提示无法启动Microsoft Outlook。无法打开此文件集合。到 Microsoft Exchange的登录失败
  19. FaceRecognitionDotNet人脸识别项目发布到linux服务器docker容器详细教程
  20. 3.15@Pointcut的表达式-@within

热门文章

  1. mave工程中的一个类调用另一个聚合工程的一个类_信息系统管理工程师备考分享(材料重点精炼)——第一章信息化和信息系统(4)...
  2. 大数据 清华 覃征_2021年清华(清华大学)大数据工程考研难度解析、考研经验分享...
  3. macos big sur升级失败_【王牌出击】升级欲望更为强烈 斯旺西有望反客为主
  4. 【若依(ruoyi)】启动时,卡死在:Using existing EHCache named [loginRecordCache]
  5. 淘宝NPM镜像、cnmp
  6. 【CentOS】EOF使用
  7. android xml 未能解析文件,Android Studio中“无法解析符号R”
  8. python删除空白没有显示_删除Python字符串中的空白
  9. php调mysql接口头文件_php基础系列:PHP连接MySQL数据库用到的三种API
  10. 学而思的python课怎么样_有在用学而思网校的同学觉得孙墨漪老师怎么样?报她的课值得吗?...