Invariant violation nothing was returnded from render - react-native

renderAvail(user, sectionId, rowId, highlightRow){
return(
<ImageBackground source={require('../../../asset/available_card_medium.png')} style={styles.containerA}>
if(user.type == 'Wear'){
return(
<Image
source={require('../../../icon.png')}
resizeMode = 'cover' style={styles.thumbImage}>)
}
<View style={styles.body}>
<Text style={styles.message1}>{user.desc}</Text>
<Text style={styles.message1}>+{user.points} <Image source={require('../../../icon_large.png')} resizeMode ='contain' style ={{height:50,width:60}}/></Text>
</View>
<ImageButton
appearance={{
normal: require("../../../asset/btn.png"),
highlight: require("../../../asset/btn2.png")}}
onPress={ this.onPressButton }/>
</ImageBackground>
);
}
{
id: 0,
type: 'Survey',
desc: 'Keep Your Apple Watch on for 8 Hours Today',
title: 'Test1',
date: '09/06/2018',
},
{
id: 1,
type: 'Wear',
desc: 'Keep Your Apple Watch on for 8 Hours Today',
title: 'Test1',
date: '09/06/2018',
},
{
id: 2,
type: 'meal',
desc: 'Keep Your Apple Watch on for 8 Hours Today',
title: 'Test1',
date: '09/06/2018',
},
{
id: 3,
type: 'Survey',
desc: 'Keep Your Apple Watch on for 8 Hours Today',
title: 'Test1',
date: '09/06/2018',
},
{
id: 4,
type: 'Wear',
desc: 'Keep Your Apple Watch on for 8 Hours Today',
title: 'Test1',
date: '09/06/2018',
}
];

You cant return use return directly inside a return block because it will be already returned if condition meets. instead of above code, you can use this.
return (<ImageBackground source={require('../../../asset/available_card_medium.png')} style={styles.containerA}>
{user.type == 'Wear' && <Image
source={require('../../../icon.png')}
resizeMode='cover' style={styles.thumbImage}>)
}
<View style={styles.body}>
<Text style={styles.message1}>{user.desc}</Text>
<Text style={styles.message1}>+{user.points} <Image source={require('../../../icon_large.png')} resizeMode='contain' style={{ height: 50, width: 60 }} /></Text>
</View>
<ImageButton
appearance={{
normal: require("../../../asset/btn.png"),
highlight: require("../../../asset/btn2.png")
}}
onPress={this.onPressButton} />
</ImageBackground>
);

Going through your code i can understand that you are new to react native.
As #Ranvir Gorai answered previously, you can not use if statement inside a render(). please spare some time and go through JSX elements.
Assuming you are calling a function renderAvail() from render of a class.
import React from 'react';
import {
View,
Text,
// add other elements which are used.
} from 'react-native';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
user:'some data',
sectionId:'some data',
rowId: 'some data',
highlightRow: 'some data',
}
}
// add the data to state using this.setState({key,value});
renderAvail() {
return (
<ImageBackground source={require('../../../asset/available_card_medium.png')} style={styles.containerA}>
{user.type == 'Wear' && <Image
source={require('../../../icon.png')}
resizeMode='cover' style={styles.thumbImage}>)
}
<View style={styles.body}>
<Text style={styles.message1}>{user.desc}</Text>
<Text style={styles.message1}>+{user.points} <Image source={require('../../../icon_large.png')} resizeMode='contain' style={{ height: 50, width: 60 }} /></Text>
</View>
<ImageButton
appearance={{
normal: require("../../../asset/btn.png"),
highlight: require("../../../asset/btn2.png")
}}
onPress={this.onPressButton} />
</ImageBackground>
);
}
render() {
return (
{this.renderAvail(this.state.user, this.state.sectionId, this.state.rowId, this.state.highlightRow)}
);
}
}

Related

Add a button "see more" in FlatList?

I use flatList to make a list of elements. I would like to show 15 elements and then add a button "see more" to show the next 15 etc.
I was about tu use this tutorial : https://aboutreact.com/react-native-flatlist-pagination-to-load-more-data-dynamically-infinite-list/
But I don't need to use fetch, I already have set up the data (state.listData) and in fact, I'm a little lost on how to adapt it...
I thought that maybe anyone could help me a little.
Thanks a lot
this.state = {
selectedId: '',
setSelectedId:'',
listData:''
}
};
renderItem = ({ item }) => {
const backgroundColor = item.id === this.selectedId ? "transparent" : "fff";
return (
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<Item
item={item}
onPress={() => this.props.navigation.navigate('UpdateTripsForm')}
style={{ backgroundColor }}
/>
<Image source={require("../../assets/images/arrow.png")} style={{width: 15, height:15, justifyContent: 'center'}}/>
</View>
);
};
initListData = async () => {
let list = await getFlights(0);
if (list) {
this.setState({
listData: list
});
}
};
render() {
return (
<SafeAreaView style={styles.container}>
<FlatList
data={this.state.listData}
renderItem={this.renderItem}
maxToRenderPerBatch={15}
keyExtractor={(item) => item.id}
extraData={this.selectedId}
/>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
{i18n.t("tripsform.action.back")}
</Text>
</View>
<Image
source={require("../../assets/images/btn-background.png")}
style={styles.tripsimg2}
/>
</TouchableOpacity>
</SafeAreaView>
);
};
}
I just tried this thanks to #Pramod 's answer :
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} style={[styles.flightsListitem, style]}>
<Text style={styles.h4}>{item.id}</Text>
</TouchableOpacity>
);
export default class FlightsList extends Component {
constructor(props) {
super(props);
this.state = {
selectedId: '',
setSelectedId:'',
listData:'',
page:1,
perPage:2,
loadMoreVisible:true,
displayArray:[]
}
};
renderItem = ({ item }) => {
const backgroundColor = item.id === this.selectedId ? "transparent" : "fff";
return (
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<Item
item={item}
onPress={() => this.props.navigation.navigate('UpdateTripsForm')}
style={{ backgroundColor }}
/>
<Image source={require("../../assets/images/arrow.png")} style={{width: 15, height:15, justifyContent: 'center'}}/>
</View>
);
};
initListData = async () => {
let list = await getFlights(0);
if (list) {
this.setState({
listData: list
});
}
};
componentDidMount(){
this.setNewData()
// console.log(tempArray)
}
setNewData(){
var tempArray=[]
if(this.state.listData.length == this.state.displayArray.length){
this.setState({
loadMoreVisible:false
})
}else{
for(var i=0; i<(this.state.page*this.state.perPage); i++){
tempArray.push(this.state.listData)
}
this.setState({
displayArray: tempArray,
loadMoreVisible:true
})
}
}
loadMore(){
this.setState({
page: this.state.page+1
},()=>{
this.setNewData()
})
}
async UNSAFE_componentWillMount() {
this.initListData();
}
render() {
return (
<ImageBackground
source={require("../../assets/images/background.jpg")}
style={styles.backgroundImage}
>
<Header
backgroundImage={require("../../assets/images/bg-header.png")}
backgroundImageStyle={{
resizeMode: "stretch",
}}
centerComponent={{
text: i18n.t("mytrips.title"),
style: styles.headerComponentStyle,
}}
containerStyle={[styles.headerContainerStyle, { marginBottom: 0 }]}
statusBarProps={{ barStyle: "light-content" }}
/>
<SafeAreaView style={styles.container}>
<FlatList
data={this.state.displayArray}
renderItem={this.renderItem}
keyExtractor={(item) => item.id}
extraData={this.selectedId}
/>
{this.state.loadMoreVisible == true?
<Button style={{width:'100%', height:10, backgroundColor:'green', justifyContent:'center', alignItems:'center'}}
title = 'load more'
onPress={()=>{this.loadMore()}}>
</Button>:null}
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
{i18n.t("tripsform.action.back")}
</Text>
</View>
<Image
source={require("../../assets/images/btn-background.png")}
style={styles.tripsimg2}
/>
</TouchableOpacity>
</SafeAreaView>
</ImageBackground>
);
};
}
the flatlist is not displayed : I get :
You can user pagination method with per page limit so that you can have granular control
Load the array per page when component mount
On every click increase the per page and based on per page update data of your flat list
And also put a flag which will check when the data has ended which will help to hide the load more button when data ends
Working example: https://snack.expo.io/#msbot01/suspicious-orange
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
SafeAreaView,
SectionList,
Switch,
FlatList
} from 'react-native';
import Constants from 'expo-constants';
import Icon from 'react-native-vector-icons/FontAwesome';
import AwesomeIcon from 'react-native-vector-icons/FontAwesome';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
page:1,
perPage:2,
loadMoreVisible:true,
DATA: [{
id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
title: 'First Item',
},
{
id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
title: 'Second Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d72',
title: 'Third Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d72',
title: 'fourth Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d72',
title: 'fifth Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29sd72',
title: 'sixth Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29dr72',
title: 'seventh Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d7w2',
title: 'Eight Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29ad72',
title: 'Nineth Item',
},
{
id: '58694a0f-3da1-471f-bd96-14557d1e29d72',
title: 'Tenth Item',
}],
displayArray:[]
}
}
componentDidMount(){
this.setNewData()
// console.log(tempArray)
}
setNewData(){
var tempArray=[]
if(this.state.DATA.length == this.state.displayArray.length){
this.setState({
loadMoreVisible:false
})
}else{
for(var i=0; i<(this.state.page*this.state.perPage); i++){
tempArray.push(this.state.DATA[i])
}
this.setState({
displayArray: tempArray,
loadMoreVisible:true
})
}
}
loadMore(){
this.setState({
page: this.state.page+1
},()=>{
this.setNewData()
})
}
render() {
return (
<View style={{ flex: 1 }}>
<FlatList
data={this.state.displayArray}
renderItem={({item})=>
<View style={{flexDirection:'row'}}>
<Text style={{fontSize:20}}>{item.title} </Text>
</View>
}
keyExtractor={item => item.id}
/>
{this.state.loadMoreVisible == true?
<View style={{width:'100%', height:10, backgroundColor:'green', justifyContent:'center', alignItems:'center'}} onClick={()=>{this.loadMore()}}>Load more</View>:null
}
</View>
);
}
}
Set data in state (already done ==> this.state.listData)
Set counter in state (initialize with 1)
Set 15 first elements in state (you can name it "renderedData" or something like that) and then increase cuonter to 1
Add a function that increases the "renderedData" by 15 items by increasing the counter by one
Add Footer component to the list which will call the function you created in stage 3
To take only 15( or 30/45/60 etc..) items from the list you can do something like this:
this.setState({ renderedItem: listData.slice(0, counter*15) })

Breakline after a item of an array reactnative

I have item that are in array. I want to change the line after setting the first item and so on. I am getting the values in a row. But I want it in column. I am getting value as:
015245088 9823178404 9851108404
But I want value as:
015245088
9823178404
9823178404
I have implemented as follows:
this.state = {
contact: [
{
id: 0,
name: '015245088'
},
{
id: 1,
name: '9823178404'
},
{
id: 2,
name: '9851108404'
}
]
}
<CardSection>
<FontAwesomeIcon style={styles.contentStyle} icon={faPhone} />
{
this.state.contact.map((item, index) => (
<TouchableOpacity
key={item.id}
style={styles.opacityStyle}
onPress={()=>Linking.openURL(`tel:${item.name}`)}>
<Text style={styles.contactStyle}>{item.name} </Text>
</TouchableOpacity>
))
}
</CardSection>
this.state = {
contact: [
{
id: 0,
name: '015245088'
},
{
id: 1,
name: '9823178404'
},
{
id: 2,
name: '9851108404'
}
]
}
<CardSection>
<FontAwesomeIcon style={styles.contentStyle} icon={faPhone} />
<View style={{flexDirection:'column'}}>
{
this.state.contact.map((item, index) => (
<TouchableOpacity
key={item.id}
style={styles.opacityStyle}
onPress={()=>Linking.openURL(`tel:${item.name}`)}>
<Text style={styles.contactStyle}>{item.name} </Text>
</TouchableOpacity>
}
</View>
</CardSection>
You need to change the flexDirection to column.
So, Just add the following styles to styles.contentStyle. Or wrap your map function with a View and add the following styles to it.
flexDirection: 'column'
flex: 1

How to make only one user on the left side and others on the right side in chat app with react-native-gifted-chat

I'm developing a mobile chat app, and this is like a group chat.
The comments of the room owner should be on the left side in the chat screen, and the comments of others should be on the right side in it.
Example: The comments of user id 1 should be left side in the chat, the comments of user id 2~100 should be right side in the chat.
I think I should use renderMessage to custom the chat message UI. But I don't understand how to use it effectively.
I have already tried to develop renderMessage and render like below.
import React, { Component } from 'react';
import { Image, ImageBackground, Text, Linking, SafeAreaView, ScrollView, StyleSheet, View } from 'react-native';
import { Body, Container, Header, Icon, Left, Right, Thumbnail } from "native-base";
import { Button, List } from 'react-native-paper';
import { GiftedChat, Composer } from 'react-native-gifted-chat';
export default class ChatScreen extends Component {
messages = [
{
_id: 4,
text: <Text onPress={() => ( alert('Hello'))} style={{ color: 'red' }}>Sample</Text>,
createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
user: {
_id: 1,
name: 'Chat owner'
}
},
{
_id: 3,
text: <Text onPress={() => ( alert('Hello'))} style={{ color: 'red' }}>Sample</Text>,
createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
system: true,
// Any additional custom parameters are passed through
},
{
_id: 2,
text: <Text onPress={() => { alert('hello')}} style={{ color: 'red' }}>Sample</Text>,
createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
system: true,
// Any additional custom parameters are passed through
},
{
_id: 1,
text: 'This is a quick reply. Do you love Gifted Chat? (radio) KEEP IT',
createdAt: new Date(),
user: {
_id: 2,
name: 'Chat user',
},
},
];
renderComposer = props => {
return (
<View style={{flexDirection: 'row'}}>
<Icon type='SimpleLineIcons' name='paper-clip' style={{ fontSize: 20, justifyContent: 'center', paddingTop: 10, paddingLeft: 8 }}/>
<Composer {...props} />
<Button>Submit</Button>
</View>
);
};
renderMessage = props => {
return (
<View style={{ paddingLeft: 10 }} >
<Thumbnail small source={require('../../assets/thumbnail.png')} style={{ justifyContent: 'center' }}/>
</View>
);
};
onSend = (messages = []) => {
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messages),
}))
};
render() {
return (
<Container>
<GiftedChat
renderComposer={this.renderComposer}
renderMessage={this.renderMessage}
onSend={(messages) => this.onSend(messages)}
messages={this.messages}
placeholder=''
loadEarlier={true}
showAvatarForEveryMessage={true}
renderUsernameOnMessage={true}
/>
</Container>
)
}
}
Now, all messages are on the left side in the chat like below.
https://gyazo.com/bfe08a8f648f3d4648a8d6d26556b116
Of course, all messages on the left side in the chat because the Thumbnail is placed on the left side.
I would like to know how to fetch the user id from the message in the 'renderMessage'.
If I know it, I will develop the code like below.
renderMessage = props => {
paddingLeft = (userId === 1 ? '10' : '200') // I will add this code
return (
<View style={{ paddingLeft: paddingLeft }} >
<Thumbnail small source={require('../../assets/thumbnail.png')} style={{ justifyContent: 'center' }}/>
</View>
);
};
try adding the user who is writting to the giftedChat:
<GiftedChat
isAnimated
messages={this.props.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: '200',
name: 'faisal',
}}
/>
Every message that has the user's _id: '200' will be displayed on the right.
example of message that will be displayed on the right side:
{
_id: 5,
text: 'Hello World?',
createdAt: new Date(),
user: {
_id: '200',
name: 'fasial'
},
},
Make sure you're not passing integer (parseInt()) values to _id fields. Just stringify any integer ids. Example in the message..
{
_id: id.toString(),
text: message,
createdAt: new Date(),
user: { _id: userId.toString(), name: name }
}
And then in the render()
isAnimated
messages={messages}
onSend={newMessages => this._onSend(newMessages)}
user={{
_id: id.toString(),
name: name
}}
renderMessages = (msg) => {
let message = msg.currentMessage
var ColorCode = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
var date = moment(message.timestamp)
.utcOffset('+05:30')
.format('hh:mm A');
console.log("single message", message)
return (
<View>
{/* <Text>{item.message}</Text> */}
{message.user.user_id === this.state.login_response.id ?
<View style={styles.left_bubble}>
{/* <Text>{message.user.name}</Text> */}
<Text>{message.text}</Text>
<Text style={{alignSelf:'flex-end',color:'#8c8c8c'}}>{date}</Text>
</View>
:
<View style={styles.rightBubble}>
<Text style={{ color: ColorCode }}>{message.user.name}</Text>
<Text style={{ padding: 2 }}>{message.text}</Text>
<Text style={{fontSize:TextFormatter.normalize(12), alignSelf:'flex-end',color:'#8c8c8c'}}>{date}</Text>
</View>
}
</View>
)}
<GiftedChat
messages={this.state.messages}
onSend={Fire.shared.send}
user={this.user}
renderDay={this.renderDay}
renderMessage={(message) => this.renderMessages(message)}
/>

Remove item from a list in react-native

I'm displaying a list of items on a page which i provide users the ability to delete.
With my code when the users taps on delete i get this error
undefined is not an object (evaluating this.state.myGroups)
JS
handleExistGroup(item) {
var array = [...this.state.myGroups];
var index = array.indexOf(item.target.value)
if (index !== -1) {
array.splice(index, 1);
this.setState({myGroups: array});
}
}
Array
state = {
myGroups : [
{
name: 'Karate Group',
description: 'Test Group',
icon: 'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg'
},
{
name: 'Choir Group',
description: 'Test 2',
icon: 'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg'
}
]
}
VIEW
<View style={styles.container}>
<ScrollView >
{
groupList.map((item, i) => {
return (
<View key={i} style={styles.user}>
<Card >
<ListItem
roundAvatar
title={item.name}
avatar={{uri:'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg'}}
/>
<View>
<Button
containerViewStyle={{width:'50%', alignSelf:'flex-end', position:"absolute", top:0, right:-25}}
onPress={()=>(handleExistGroup(item))}
rounded = {true}
style={{margin:10}}
icon={{name: 'trash'}}
backgroundColor='#DC143C'
buttonStyle={{borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0}}
title='Exit Group'
/>
</View>
<Text style={{alignSelf:'center', padding:5, fontFamily:'HelveticaNeue-Light', fontSize:16}}>
Jonied: 24th January, 2019
</Text>
</Card>
</View>
);
})
}
</ScrollView>
</View>
How do i make it work so it can delete the particular row a use want to delete from the array?
You need to bind handleExistGroup() function with this in your constructor.
constructor(props) {
super(props);
this.handleExistGroup = this.handleExistGroup.bind(this);
}

React native cannot display flat list

I want to display the FlatList that I created. I want to show the title and artist that is in my data. There is no error but my output would not appear anything.
This is my data:
var track =
[
{ List: 'list1',
data:
[
{id: '1', url: 'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',title: 'Despacito',artist:'Luis Fonsi'},
{id: '2', url: 'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',title: 'YoungBlood',artist:'5SOS'},
]
},
{ List: 'list2',
data:
[
{id: '1111', url: 'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',title: 'Summertime',artist:'Yelow Claw'},
{id: '2222', url: 'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',title: 'Better Now',artist:'Post Malone'},
]},
];
module.exports = {track:track};
And this is my FlatList:
export default class SongList extends Component{
render(){
const { navigate } = this.props.navigation;
return(
<View>
<FlatList
data={track}
renderItem={({item,index})=>{
return(
<FlatListItem item={item} index={index}>
</FlatListItem>);
}}
>
</FlatList>
</View>
);
}
}
class FlatListItem extends Component{
render(){
return(
<View style={styles.list}>
<View>
<Text style={styles.itemTitle}>{this.props.item.title}</Text>
<Text style={styles.itemArtist}>{this.props.item.artist}</Text>
</View>
</View>
);
}
}
Basically I am able to run the program. But there is nothing showing up on the screen. There is only a blank background.
const styles = StyleSheet.create({
itemArtist:{
textAlign: 'center',
justifyContent: 'center',
fontSize: 23,
borderBottomWidth: 4,
borderBottomColor: '#ccc',
marginTop: 10,
padding: 10,
color: 'blue',
},
itemTitle:{
textAlign: 'center',
justifyContent: 'center',
fontSize: 23,
borderBottomWidth: 4,
borderBottomColor: '#ccc',
marginTop: 10,
padding: 10,
color: 'blue',
},
list:{
flex:1,
}
You should write your code as below:
Your data:
export default [
{
List: 'list1',
data: [
{
id: '1',
url: 'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',
title: 'Despacito',
artist: 'Luis Fonsi',
},
{
id: '2',
url: 'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',
title: 'YoungBlood',
artist: '5SOS',
},
],
},
{
List: 'list2',
data: [
{
id: '1111',
url: 'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',
title: 'Summertime',
artist: 'Yelow Claw',
},
{
id: '2222',
url: 'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',
title: 'Better Now',
artist: 'Post Malone',
},
],
},
];
Import your data like this, I assume the data file and your code are in a same folder:
import track from './data';
Your components:
export default class App extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View>
<FlatList
data={track}
renderItem={({ item, index }) => {
return <FlatListItem item={item} index={index} />;
}}
/>
</View>
);
}
}
class FlatListItem extends Component {
render() {
return (
<View style={styles.list}>
<View>
<Text style={styles.itemTitle}>{this.props.item.data[0].title}</Text>
<Text style={styles.itemArtist}>{this.props.item.data[0].artist}</Text>
</View>
<View>
<Text style={styles.itemTitle}>{this.props.item.data[1].title}</Text>
<Text style={styles.itemArtist}>{this.props.item.data[1].artist}</Text>
</View>
</View>
);
}
}
And if you want to show only the data of list1 in your FlatList you should change your code as below:
export default class App extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View>
<FlatList
data={track[0].data}
renderItem={({ item, index }) => {
return <FlatListItem item={item} index={index} />;
}}
/>
</View>
);
}
}
class FlatListItem extends Component {
render() {
return (
<View style={styles.list}>
<View>
<Text style={styles.itemTitle}>{this.props.item.title}</Text>
<Text style={styles.itemArtist}>{this.props.item.artist}</Text>
</View>
</View>
);
}
}
You can make your data schema better to show them better too.
Steps to debug :-
try to console track, check if data is coming properly.
add console in componentDidMount and check if item if coming there or not.
Add keyExtractor in flatlist, keyExtractor tells the list to use the ids for the react keys instead of the default key property.
<FlatList
data={track}
keyExtractor={(item, index) => ""+index}
renderItem={({ item, index }) => {
return <FlatListItem item={item} index={index} />;
}}
/>