-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
84 lines (62 loc) · 2.07 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, { useEffect, useState } from 'react';
import { Text, View, StyleSheet, SafeAreaView, TextInput, Button } from 'react-native';
import { Socket } from 'socket.io-client';
import socketServices from './src/utils/socketService';
const App = () => {
const [message, setmessage] = useState('')
const [data, setData] = useState([])
useEffect(() => {
socketServices.initializesocket()
}, [])
useEffect(() => {
socketServices.on('received_message', (msg) => {
console.log('messgae received in app', msg)
let cloneArry = [...data]
setData(cloneArry.concat(msg))
})
}, [data])
const sendMessage = () => {
if (!!message) {
socketServices.emit('send_message', message)
setmessage('')
return;
}
alert("Please Enter Your Message")
}
return (
<SafeAreaView style={{ flex: 3 }}>
<View style={styles.container}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<View style={{ flex: 0.8 }}>
<TextInput
value={message}
placeholder='Enter Your Message'
style={styles.inputStyle}
onChangeText={text => setmessage(text)} />
</View>
<View style={{ flex: 0.2 }}>
<Button onPress={sendMessage} color="#000000" title='Send' />
</View>
</View>
{data.map((val, i) => {
return (
<Text style={{ marginBottom: 8, fontWeight: 'bold' }}>{val}</Text>
)
})}
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 3,
padding: 24
},
inputStyle: {
height: 42,
borderWidth: 3,
borderRadius: 6,
paddingHorizontal: 8
},
});
export default App;