List item for chat from Array inside array React Native












0














This is my code for Chat Box, the window where I have "In" and "Out" messages are appearing.






import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});





I am using axios to fetch the api, the api response is coming as an Array inside the array but nothing is coming on the screen.



I can get it with storing response upto message but then I can't have the loop on messages.



Please help.



Thanks in advance










share|improve this question






















  • Can you share the response of service also?
    – Madhavan.V
    Nov 13 '18 at 7:42










  • prntscr.com/lhp48m please have a look
    – Kedar Dave
    Nov 13 '18 at 7:49
















0














This is my code for Chat Box, the window where I have "In" and "Out" messages are appearing.






import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});





I am using axios to fetch the api, the api response is coming as an Array inside the array but nothing is coming on the screen.



I can get it with storing response upto message but then I can't have the loop on messages.



Please help.



Thanks in advance










share|improve this question






















  • Can you share the response of service also?
    – Madhavan.V
    Nov 13 '18 at 7:42










  • prntscr.com/lhp48m please have a look
    – Kedar Dave
    Nov 13 '18 at 7:49














0












0








0







This is my code for Chat Box, the window where I have "In" and "Out" messages are appearing.






import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});





I am using axios to fetch the api, the api response is coming as an Array inside the array but nothing is coming on the screen.



I can get it with storing response upto message but then I can't have the loop on messages.



Please help.



Thanks in advance










share|improve this question













This is my code for Chat Box, the window where I have "In" and "Out" messages are appearing.






import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});





I am using axios to fetch the api, the api response is coming as an Array inside the array but nothing is coming on the screen.



I can get it with storing response upto message but then I can't have the loop on messages.



Please help.



Thanks in advance






import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});





import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
FlatList,
Platform,
AsyncStorage
} from "react-native";
import Tutor from "../image/krutika.jpg";
import {
Container,
Header,
Left,
Input,
Body,
Right,
Thumbnail,
Button
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from "react-native-vector-icons/Ionicons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import axios from "axios";

export default class ChatBox extends Component {
static navigationOptions = {
header: null
};
state = {
group_msgs: ,
student_id: null
};


renderDate = date => {
return <Text style={styles.time}>{date}</Text>;
};

componentWillMount = () => {
this.loading();
const { navigation } = this.props;
groupName = navigation.getParam("groupName");
group_id = navigation.getParam("group_id");
};

loading = async () => {
const userid = await AsyncStorage.getItem("user_id");
this.state.student_id = userid;

try {
let { data } = await axios.get('https://www.qualpros.com/chat/imApi/getMessage?groupId=6&limit=10&start=0&userId=62').then(response => {
// console.log(response)
if (response.status == 200) {
this.setState({ group_msgs: response.data.response.message });
console.log(response.data.response)
} else {
}
});
} catch (err) {
console.log(err);
}
};
render() {
return (
<Container>
<Header style={{ backgroundColor: "#d91009" }}>
<Left style={{ flex: 1, flexDirection: "row" }}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("ChatScreen")}
>
<FontAwesome name="angle-left" size={30} color="#fff" />
</TouchableOpacity>

<Thumbnail
source={Tutor}
style={{
marginLeft: 8,
width: 30,
height: 30,
borderRadius: 30 / 2
}}
/>
</Left>
<Body>

<Text
onPress={() => {

this.props.navigation.navigate("Groupmembers", {
group_id:group_id,
groupname:groupName,
});
}}
style={{
alignSelf: Platform.OS == "android" ? "center" : null,
fontSize: 17,
color: "#fff"
}}
>
{groupName}
</Text>

</Body>
<Right>
<Button
style={{ backgroundColor: "#d91009" }}
onPress={() => {
this.props.navigation.navigate("TutorCalender");
}}
>
<Icon1 active name="calendar" size={24} color="#FFF" />
</Button>
</Right>
</Header>
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.group_msgs}
keyExtractor={item => {
return item.m_id;
}}
renderItem={message => {
console.log(item);
const item = message.item;
let inMessage = (item.sender === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message}</Text>
</View>
</View>
);
}}
/>
<View style={styles.footer}>
<View style={styles.inputContainer}>
<TextInput
style={styles.inputs}
placeholder="Write a message..."
underlineColorAndroid="transparent"
onChangeText={name_address => this.setState({ name_address })}
/>
</View>

{/* <TouchableOpacity style={styles.btnSend}>
<Ionicons name="md-send" size={36} color='#d91009' /> style={styles.iconSend} />
</TouchableOpacity> */}
</View>
</View>
</Container>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingHorizontal: 17
},
footer: {
flexDirection: "row",
height: 60,
backgroundColor: "#eeeeee",
paddingHorizontal: 10,
padding: 5
},
btnSend: {
//color: "#d91009",
width: 40,
height: 40,
borderRadius: 360,
alignItems: "center",
justifyContent: "center"
},
iconSend: {
width: 30,
height: 30,
alignSelf: "center"
},
inputContainer: {
borderBottomColor: "#F5FCFF",
backgroundColor: "#FFFFFF",
borderRadius: 30,
borderBottomWidth: 1,
height: 40,
flexDirection: "row",
alignItems: "center",
flex: 1,
marginRight: 10
},
inputs: {
height: 40,
marginLeft: 16,
borderBottomColor: "#FFFFFF",
flex: 1
},
balloon: {
maxWidth: 250,
padding: 15,
borderRadius: 20
},
itemIn: {
alignSelf: "flex-start",
backgroundColor: "#eeeeee"
},
itemOut: {
alignSelf: "flex-end",
backgroundColor: "#DCF8C5"
},
time: {
alignSelf: "flex-end",
margin: 15,
fontSize: 12,
color: "#808080"
},
item: {
marginVertical: 14,
flex: 1,
flexDirection: "row",

borderRadius: 300,
padding: 1
}
});






javascript react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:32









Kedar DaveKedar Dave

142112




142112












  • Can you share the response of service also?
    – Madhavan.V
    Nov 13 '18 at 7:42










  • prntscr.com/lhp48m please have a look
    – Kedar Dave
    Nov 13 '18 at 7:49


















  • Can you share the response of service also?
    – Madhavan.V
    Nov 13 '18 at 7:42










  • prntscr.com/lhp48m please have a look
    – Kedar Dave
    Nov 13 '18 at 7:49
















Can you share the response of service also?
– Madhavan.V
Nov 13 '18 at 7:42




Can you share the response of service also?
– Madhavan.V
Nov 13 '18 at 7:42












prntscr.com/lhp48m please have a look
– Kedar Dave
Nov 13 '18 at 7:49




prntscr.com/lhp48m please have a look
– Kedar Dave
Nov 13 '18 at 7:49












1 Answer
1






active

oldest

votes


















1














When doing a GET request to the endpoint in your code the response looks as follows:



{  
"status":{
"code":200,
"message":"Success"
},
"totalMessage":6,
"recentMessageId":228,
"response":[
...
]
}


Inside the response object there's an array of message objects so you can't use response.data.response.message when setting state. That part of your code needs to be:



this.setState({ group_msgs: response.data.response });


Now you should be able to iterate through the group_msgs object to get the message key value for each item in the array.



Also in the FlatList component you should have



keyExtractor={item => {
return item.message.m_id;
}}


Your renderItem seems to be wrong as well, see should be something like this:



renderItem={ ({item}) => {
let inMessage = (item.sender.usedId === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message.message}</Text>
</View>
</View>
);
}}


I strongly suggest you take a look at the structure of the response object since that's where you are failing at the moment.






share|improve this answer























  • I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
    – Kedar Dave
    Nov 13 '18 at 7:54










  • @KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
    – Ismailp
    Nov 13 '18 at 7:56












  • can you help me how i can set it right and left according to the senderid? Userid is the key
    – Kedar Dave
    Nov 13 '18 at 8:11










  • @KedarDave hmm..you mean the position of the chat bubbles?
    – Ismailp
    Nov 13 '18 at 8:25






  • 1




    let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
    – Ismailp
    Nov 13 '18 at 8:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275944%2flist-item-for-chat-from-array-inside-array-react-native%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














When doing a GET request to the endpoint in your code the response looks as follows:



{  
"status":{
"code":200,
"message":"Success"
},
"totalMessage":6,
"recentMessageId":228,
"response":[
...
]
}


Inside the response object there's an array of message objects so you can't use response.data.response.message when setting state. That part of your code needs to be:



this.setState({ group_msgs: response.data.response });


Now you should be able to iterate through the group_msgs object to get the message key value for each item in the array.



Also in the FlatList component you should have



keyExtractor={item => {
return item.message.m_id;
}}


Your renderItem seems to be wrong as well, see should be something like this:



renderItem={ ({item}) => {
let inMessage = (item.sender.usedId === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message.message}</Text>
</View>
</View>
);
}}


I strongly suggest you take a look at the structure of the response object since that's where you are failing at the moment.






share|improve this answer























  • I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
    – Kedar Dave
    Nov 13 '18 at 7:54










  • @KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
    – Ismailp
    Nov 13 '18 at 7:56












  • can you help me how i can set it right and left according to the senderid? Userid is the key
    – Kedar Dave
    Nov 13 '18 at 8:11










  • @KedarDave hmm..you mean the position of the chat bubbles?
    – Ismailp
    Nov 13 '18 at 8:25






  • 1




    let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
    – Ismailp
    Nov 13 '18 at 8:38
















1














When doing a GET request to the endpoint in your code the response looks as follows:



{  
"status":{
"code":200,
"message":"Success"
},
"totalMessage":6,
"recentMessageId":228,
"response":[
...
]
}


Inside the response object there's an array of message objects so you can't use response.data.response.message when setting state. That part of your code needs to be:



this.setState({ group_msgs: response.data.response });


Now you should be able to iterate through the group_msgs object to get the message key value for each item in the array.



Also in the FlatList component you should have



keyExtractor={item => {
return item.message.m_id;
}}


Your renderItem seems to be wrong as well, see should be something like this:



renderItem={ ({item}) => {
let inMessage = (item.sender.usedId === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message.message}</Text>
</View>
</View>
);
}}


I strongly suggest you take a look at the structure of the response object since that's where you are failing at the moment.






share|improve this answer























  • I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
    – Kedar Dave
    Nov 13 '18 at 7:54










  • @KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
    – Ismailp
    Nov 13 '18 at 7:56












  • can you help me how i can set it right and left according to the senderid? Userid is the key
    – Kedar Dave
    Nov 13 '18 at 8:11










  • @KedarDave hmm..you mean the position of the chat bubbles?
    – Ismailp
    Nov 13 '18 at 8:25






  • 1




    let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
    – Ismailp
    Nov 13 '18 at 8:38














1












1








1






When doing a GET request to the endpoint in your code the response looks as follows:



{  
"status":{
"code":200,
"message":"Success"
},
"totalMessage":6,
"recentMessageId":228,
"response":[
...
]
}


Inside the response object there's an array of message objects so you can't use response.data.response.message when setting state. That part of your code needs to be:



this.setState({ group_msgs: response.data.response });


Now you should be able to iterate through the group_msgs object to get the message key value for each item in the array.



Also in the FlatList component you should have



keyExtractor={item => {
return item.message.m_id;
}}


Your renderItem seems to be wrong as well, see should be something like this:



renderItem={ ({item}) => {
let inMessage = (item.sender.usedId === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message.message}</Text>
</View>
</View>
);
}}


I strongly suggest you take a look at the structure of the response object since that's where you are failing at the moment.






share|improve this answer














When doing a GET request to the endpoint in your code the response looks as follows:



{  
"status":{
"code":200,
"message":"Success"
},
"totalMessage":6,
"recentMessageId":228,
"response":[
...
]
}


Inside the response object there's an array of message objects so you can't use response.data.response.message when setting state. That part of your code needs to be:



this.setState({ group_msgs: response.data.response });


Now you should be able to iterate through the group_msgs object to get the message key value for each item in the array.



Also in the FlatList component you should have



keyExtractor={item => {
return item.message.m_id;
}}


Your renderItem seems to be wrong as well, see should be something like this:



renderItem={ ({item}) => {
let inMessage = (item.sender.usedId === this.state.userid) ? 'in' : 'out';
let itemStyle = inMessage ? styles.itemIn : styles.itemOut;
return (
<View style={[styles.item, itemStyle]}>
<View style={[styles.balloon]}>
<Text>{item.message.message}</Text>
</View>
</View>
);
}}


I strongly suggest you take a look at the structure of the response object since that's where you are failing at the moment.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 8:04

























answered Nov 13 '18 at 7:52









IsmailpIsmailp

1,14742353




1,14742353












  • I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
    – Kedar Dave
    Nov 13 '18 at 7:54










  • @KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
    – Ismailp
    Nov 13 '18 at 7:56












  • can you help me how i can set it right and left according to the senderid? Userid is the key
    – Kedar Dave
    Nov 13 '18 at 8:11










  • @KedarDave hmm..you mean the position of the chat bubbles?
    – Ismailp
    Nov 13 '18 at 8:25






  • 1




    let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
    – Ismailp
    Nov 13 '18 at 8:38


















  • I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
    – Kedar Dave
    Nov 13 '18 at 7:54










  • @KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
    – Ismailp
    Nov 13 '18 at 7:56












  • can you help me how i can set it right and left according to the senderid? Userid is the key
    – Kedar Dave
    Nov 13 '18 at 8:11










  • @KedarDave hmm..you mean the position of the chat bubbles?
    – Ismailp
    Nov 13 '18 at 8:25






  • 1




    let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
    – Ismailp
    Nov 13 '18 at 8:38
















I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
– Kedar Dave
Nov 13 '18 at 7:54




I have done the same, but messages are not appearing, I think i am making a mistake in defining a flatlist if you can help me resolve that
– Kedar Dave
Nov 13 '18 at 7:54












@KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
– Ismailp
Nov 13 '18 at 7:56






@KedarDave the keyExtractor should be item.message.m_id. I've updated my answer. And it seems like your renderItem is wrong as well.
– Ismailp
Nov 13 '18 at 7:56














can you help me how i can set it right and left according to the senderid? Userid is the key
– Kedar Dave
Nov 13 '18 at 8:11




can you help me how i can set it right and left according to the senderid? Userid is the key
– Kedar Dave
Nov 13 '18 at 8:11












@KedarDave hmm..you mean the position of the chat bubbles?
– Ismailp
Nov 13 '18 at 8:25




@KedarDave hmm..you mean the position of the chat bubbles?
– Ismailp
Nov 13 '18 at 8:25




1




1




let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
– Ismailp
Nov 13 '18 at 8:38




let itemStyle = inMessage ? styles.itemIn : styles.itemOut; is your code. It is always true. So change the line above to let inMessage = (item.sender === this.state.userid) ? Boolean(true) : Boolean(false);
– Ismailp
Nov 13 '18 at 8:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275944%2flist-item-for-chat-from-array-inside-array-react-native%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine