ReactNative聊天APP实战 您所在的位置:网站首页 仿微信聊天软件搭建 ReactNative聊天APP实战

ReactNative聊天APP实战

2024-06-09 19:52| 来源: 网络整理| 查看: 265

项目简介

最近一直在研究RN技术,想着找个项目练练手,之前就有使用vue、react技术开发过聊天项目,这次就使用reactNative技术开发个仿微信RN版。是基于 react-native+react-navigation+react-redux+react-native-image-picker+rnPop 等技术实现开发的仿微信原生APP聊天项目,该项目从搭建到开发完 花了半个月,主要是白天太忙,只能晚上抽时间弄,开发过程采坑不少,不过通过一些技术论坛把问题都一 一撸顺了。

  

技术选择 RN脚手架:react / react-native / react-native-cli状态管理:react-redux页面导航:react-navigationrn 弹窗组件:rnPop打包工具:webpack 2.0轮播组件:react-native-swiper图片 /相册:react-native-image-picker { "name": "RN_ChatRoom", "aboutMe": "QQ:282310962", "dependencies": { "react": "16.8.6", "react-native": "0.60.4" }, "devDependencies": { "@babel/core": "^7.5.5", "@babel/runtime": "^7.5.5", "@react-native-community/async-storage": "^1.6.1", "@react-native-community/eslint-config": "^0.0.5", "babel-jest": "^24.8.0", "eslint": "^6.1.0", "jest": "^24.8.0", "metro-react-native-babel-preset": "^0.55.0", "react-native-gesture-handler": "^1.3.0", "react-native-image-picker": "^1.0.2", "react-native-swiper": "^1.5.14", "react-navigation": "^3.11.1", "react-redux": "^7.1.0", "react-test-renderer": "16.8.6", "redux": "^4.0.4", "redux-thunk": "^2.3.0" } }

  

  

  

  

  

  

RN中写公共样式

新建common.js公共样式库,通过react native中提供的global全局变量,在app.js中一次引入,然后所有页面均可以调用。

global.GStyle = { pixel: 1 / PixelRatio.get(), // 边框 borT: {borderTopWidth: 1 / PixelRatio.get(), borderTopColor: '#dedede',}, borB: {borderBottomWidth: 1 / PixelRatio.get(), borderBottomColor: '#dedede',}, /* __ 布局控制 */ align_l: {textAlign: 'left'}, align_c: {textAlign: 'center'}, align_r: {textAlign: 'right'}, pos_rel: {position: 'relative'}, pos_abs: {position: 'absolute'}, /* __ 颜色(背景、文字) */ bg_fff: {backgroundColor: '#fff'}, bg_45cff5: {backgroundColor: '#45cff5'}, c_fff: {color: '#fff'}, c_999: {color: '#999'}, c_45cff5: {color: '#45cff5'}, /* __ 字号 */ fs_14: {fontSize: 14}, fs_16: {fontSize: 16}, fs_20: {fontSize: 20}, fs_24: {fontSize: 24}, /* __ 字体 */ ff_ic: {fontFamily: 'iconfont'}, ff_ar: {fontFamily: 'arial'}, iconfont: {fontFamily: 'iconfont', fontSize: 16,}, /* __ 间距( 5/10/15/20/25/30/50 ) */ mt_10: {marginTop: 10}, mt_15: {marginTop: 15}, mt_20: {marginTop: 20}, mb_10: {marginBottom: 10}, mb_15: {marginBottom: 15}, mb_20: {marginBottom: 20}, /* __ 行高 */ lh_20: {lineHeight: 20}, lh_25: {lineHeight: 25}, lh_30: {lineHeight: 30}, lh_35: {lineHeight: 35}, lh_40: {lineHeight: 40}, flex1: {flex: 1}, flex2: {flex: 2}, flex_alignC: {alignItems: 'center'}, flex_alignT: {alignItems: 'flex-start'}, flex_alignB: {alignItems: 'flex-end'}, } RN实现app全屏启动制作

react native实现沉浸式效果(类似京东、淘宝首页顶部),只需把 StatusBar 设置为透明,这样状态栏和背景页面一体了。

export default class Splash extends Component{ constructor(props){ super(props) this.state = { animFadeOut: new Animated.Value(1), } } render(){ return ( {color: '#dbdbdb', fontSize: 12, textAlign: 'center',}}>RN-ChatRoom v1.0.0 ) } componentDidMount(){ storage.get('hasLogin', (err, object) => { setTimeout(() => { Animated.timing( this.state.animFadeOut, {duration: 300, toValue: 0} ).start(()=>{ this.props.navigation.navigate('Index') }) }, 1500); }) } } RN自定义顶部条headerBar

react-navigation自带的顶部导航栏一般能满足项目需求,可是一些特殊自定义化的项目,就只能自己定制开发了。如是基于react-navigation做了个自定义顶部导航组件。

支持多种属性配置,调用十分方便,

export default class HeaderBar extends Component { constructor(props){ super(props) this.state = { searchInput: '' } } render() { /** * 更新 * @param { navigation | 页面导航 } * @param { title | 标题 } * @param { center | 标题是否居中 } * @param { search | 是否显示搜索 } * @param { headerRight | 右侧 Icon 按钮 } */ let{ navigation, title, bg, center, search, headerRight } = this.props return ( {/* 返回 */}  {/* 标题 */} { !search && center ? : null } { search ? ( {this.setState({searchInput: text})}} style={styles.barSearchText} placeholder='搜索' placeholderTextColor='rgba(255,255,255,.6)' /> ) : ( { title ? {title} : null } ) } {/* 右侧 */} { !headerRight ? null : headerRight.map((item, index) => { return( item.press ? item.press(this.state.searchInput) : null}> { item.type === 'iconfont' ? item.title : ( typeof item.title === 'string' ? {`${item.title}`} :


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有