React Native聊天app实例|RN版聊天室

不积跬步无以至千里,技术在于每天的点滴积累!

RN原生app聊天应用RN_chatRoom,基于react-native+react-navigation+react+redux+react-native-image-picker等技术实现的仿微信界面聊天实例。实现了消息发送、textInput文本框插入表情符、表情大图gif、图片选择预览、红包、朋友圈等功能。

项目中使用到的弹窗插件:rnPop自定义模态组件|msg信息框|msg消息提示|alert对话框

使用技术:

  • MVVM框架:react / react-native / react-native-cli
  • 状态管理:react-redux
  • 页面导航:react-navigation
  • rn弹窗组件:rnPop
  • 打包工具:webpack 2.0
  • 轮播组件:react-native-swiper
  • 图片/相册:react-native-image-picker
{
  "name": "RN_ChatRoom",
  "aboutMe": "QQ:282310962 、 wx:xy190310",
  
  "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"
  }
}

基于react-navigation导航器自定义顶部导航条headerBar组件

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]}>&#xe63f;</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()
    }
}
//右侧图标(文字)
<HeaderBar navigation={this.props.navigation} title='新的朋友' headerRight={[
	{
		title: '添加好友',
		style: {color: '#fff', fontSize: 14},
		press: this.handlePress01
	}
]} />

//右侧图标(iconfont)
<HeaderBar navigation={this.props.navigation} title='标题信息' headerRight={[
	{
		title: (<Text style={[GStyle.iconfont, GStyle.c_fff, GStyle.fs_18]}>&#xe653;</Text>),
		type: 'iconfont',
		press: this.handlePress01
	}
]} />

//右侧图标(图片)
<HeaderBar navigation={this.props.navigation} search headerRight={[
	{
		title: require('../../assets/img/search.png'),
		press: this.handlePress01
	},
	{
		title: require('../../assets/img/add.png'),
		press: this.handlePress02
	},
]} />

◆ react-native实现全屏幕启动页,可自定义背景图

/**
 * @desc 启动页面
 */

import React, { Component } from 'react'
import { StatusBar, Animated, View, Text, Image } from 'react-native'

export default class Splash extends Component{
    constructor(props){
        super(props)
        this.state = {
            animFadeIn: new Animated.Value(0),
            animFadeOut: new Animated.Value(1),
        }
    }

    render(){
        return (
            <Animated.View style={[GStyle.flex1DC_a_j, {backgroundColor: '#1a4065', opacity: this.state.animFadeOut}]}>
                <StatusBar backgroundColor='transparent' barStyle='light-content' translucent={true} />

                <View style={GStyle.flex1_a_j}>
                    <Image source={require('../assets/img/ic_default.jpg')} style={{borderRadius: 100, width: 100, height: 100}} />
                </View>
                <View style={[GStyle.align_c, {paddingVertical: 20}]}>
                    <Text style={{color: '#dbdbdb', fontSize: 12, textAlign: 'center',}}>RN-ChatRoom v1.0.0</Text>
                </View>
            </Animated.View>
        )
    }

    componentDidMount(){
        // 判断是否登录
        storage.get('hasLogin', (err, object) => {
            setTimeout(() => {
                Animated.timing(
                    this.state.animFadeOut, {duration: 300, toValue: 0}
                ).start(()=>{
                    // 跳转页面
                    util.navigationReset(this.props.navigation, (!err && object && object.hasLogin) ? 'Index' : 'Login')
                })
            }, 1500);
        })
    }
}

◆ 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

表情则是使用的emoj表情符,由于使用image表情的话,处理起来麻烦,也影响系统性能。

faceList: [
    {
        nodes: [
            '🙂','😁','😋','😎','😍','😘','😗',
            '😃','😂','🤣','😅','😉','😊','🤗',
            '🤔','😐','😑','😶','🙄','😏','del',
        ]
    },
    ...
]

react-navigation底部导航栏tabbar实现

// 创建底部TabBar
const tabNavigator = createBottomTabNavigator(
    // tabbar路由(消息、通讯录、我)
    {
        Index: {
            screen: Index,
            navigationOptions: ({navigation}) => ({
                tabBarLabel: '消息',
                tabBarIcon: ({focused, tintColor}) => (
                    <View>
                        <Text style={[ GStyle.iconfont, GStyle.fs_20, {color: (focused ? tintColor : '#999')} ]}>&#xe642;</Text>
                        <View style={[GStyle.badge, {position: 'absolute', top: -2, right: -15,}]}><Text style={GStyle.badge_text}>12</Text></View>
                    </View>
                )
            })
        },
        Contact: {
            screen: Contact,
            navigationOptions: {
                tabBarLabel: '通讯录',
                tabBarIcon: ({focused, tintColor}) => (
                    <View>
                        <Text style={[ GStyle.iconfont, GStyle.fs_20, {color: (focused ? tintColor : '#999')} ]}>&#xe640;</Text>
                    </View>
                )
            }
        },
        Ucenter: {
            screen: Ucenter,
            navigationOptions: {
                tabBarLabel: '我',
                tabBarIcon: ({focused, tintColor}) => (
                    <View>
                        <Text style={[ GStyle.iconfont, GStyle.fs_20, {color: (focused ? tintColor : '#999')} ]}>&#xe61e;</Text>
                        <View style={[GStyle.badge_dot, {position: 'absolute', top: -2, right: -6,}]}></View>
                    </View>
                )
            }
        }
    },
    // tabbar配置
    {
        ...
    }
)

最近附上两个项目实例,希望能喜欢😍😍 ~~~

Vue网页版聊天室:https://cloud.tencent.com/developer/article/1420150

angular聊天室:https://cloud.tencent.com/developer/article/1464674

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券