介绍
RN全家桶技术react+react-native+react-navigation+react-redux+react-native-swiper+rnPop实现的仿微信app聊天界面,实现了全屏Animated动画启动页、自定义rnPop对话框功能、消息触摸列表、发送消息/表情,图片预览,拍摄图片、发红包、仿微信朋友圈等功能。
技术实现
- rn框架:react / react-native / react-native-cli
- 状态管理:react-redux / redux
- 页面导航:react-navigation
- rn弹窗组件:rnPop
- 打包工具:webpack 2.0
- 轮播组件:react-native-swiper
- 图片/相册:react-native-image-picker
预览
{
"name": "RN_ChatRoom",
"aboutMe": "QQ:282310962 、 wx:xy190310",
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"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"
}
}
App主页面及全局路由页面配置
import React, { Fragment, Component } from 'react'
import { StatusBar } from 'react-native'
// 引入公共js
import './src/utils/util'
import './src/utils/storage'
// 导入样式
import './src/assets/css/common'
// 导入rnPop弹窗
import './src/assets/js/rnPop/rnPop.js'
// 引入页面路由
import PageRouter from './src/router'
class App extends Component{
render(){
return (
<Fragment>
{/* <StatusBar backgroundColor={GStyle.headerBackgroundColor} barStyle='light-content' /> */}
{/* 页面 */}
<PageRouter />
{/* 弹窗模板 */}
<RNPop />
</Fragment>
)
}
}
export default App
react-navigation页面导航器/地址路由、底部tabbar
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 (
<View style={GStyle.flex_col}>
<StatusBar backgroundColor={bg ? bg : GStyle.headerBackgroundColor} barStyle='light-content' translucent={true} />
<View style={[styles.rnim__topBar, GStyle.flex_row, {backgroundColor: bg ? bg : GStyle.headerBackgroundColor}]}>
{/* 返回 */}
<TouchableOpacity style={[styles.iconBack]} activeOpacity={.5} onPress={this.goBack}><Text style={[GStyle.iconfont, GStyle.c_fff, GStyle.fs_18]}></Text></TouchableOpacity>
{/* 标题 */}
{ !search && center ? <View style={GStyle.flex1} /> : null }
{
search ?
(
<View style={[styles.barSearch, GStyle.flex1, GStyle.flex_row]}>
<TextInput onChangeText={text=>{this.setState({searchInput: text})}} style={styles.barSearchText} placeholder='搜索' placeholderTextColor='rgba(255,255,255,.6)' />
</View>
)
:
(
<View style={[styles.barTit, GStyle.flex1, GStyle.flex_row, center ? styles.barTitCenter : null]}>
{ title ? <Text style={[styles.barCell, {fontSize: 16, paddingLeft: 0}]}>{title}</Text> : null }
</View>
)
}
{/* 右侧 */}
<View style={[styles.barBtn, GStyle.flex_row]}>
{
!headerRight ? null : headerRight.map((item, index) => {
return(
<TouchableOpacity style={[styles.iconItem]} activeOpacity={.5} key={index} onPress={()=>item.press ? item.press(this.state.searchInput) : null}>
{
item.type === 'iconfont' ? item.title : (
typeof item.title === 'string' ?
<Text style={item.style ? item.style : null}>{`${item.title}`}</Text>
:
<Image source={item.title} style={{width: 24, height: 24, resizeMode: 'contain'}} />
)
}
{/* 圆点 */}
{ item.badge ? <View style={[styles.iconBadge, GStyle.badge]}><Text style={GStyle.badge_text}>{item.badge}</Text></View> : null }
{ item.badgeDot ? <View style={[styles.iconBadgeDot, GStyle.badge_dot]}></View> : null }
</TouchableOpacity>
)
})
}
</View>
</View>
</View>
)
}
goBack = () => {
this.props.navigation.goBack()
}
}
RN配合emoj表情符,实现图文混排效果
faceList: [
{
nodes: [
'🙂','😁','😋','😎','😍','😘','😗',
'😃','😂','🤣','😅','😉','😊','🤗',
'🤔','😐','😑','😶','🙄','😏','del',
]
},
...
{
nodes: [
'👓','👄','💋','👕','👙','👜','👠',
'👑','🎓','💄','💍','🌂','👧🏼','👨🏼',
'👵🏻','👴🏻','👨🌾','👨🏼🍳','👩🏻🍳','👨🏽✈️','del',
]
},
...
]
过滤掉TextInput里面的空格、换行来判断内容是否为空
isEmpty = (html) => {
return html.replace(/\r\n|\n|\r/, "").replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, "") == ""
}
在指定光标处插入内容
let selection = this.textInput._lastNativeSelection || null;
this.textInput.setNativeProps({
selection : { start : xxx, end : xxx}
})
附上最近项目实例,继续搬砖去叻 ~~~
RN自定义模态组件:https://cloud.tencent.com/developer/article/1479581
vue2.0网页端聊天:https://cloud.tencent.com/developer/article/1420150
NG聊天室:https://cloud.tencent.com/developer/article/1464674