React native Flatlist data does't clear even data array is clean - react-native-flatlist

I wrote an application to show a large data with FlatList but when I use the application the Flatlist is not clear data out while the data array is empty.
renderItem = ({item}) => {
return (
<TouchableOpacity
onPress={() => this.props.navigation.navigate('Detail', { item, otherParam: item.Word1})}>
<View style={{ padding: 5, backgroundColor: '#eee'}}>
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
<DisplayField value={item.Word1} />
</View>
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
<DisplayField value={item.Word2} />
</View>
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
<DisplayField value={item.Word3} />
</View>
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
<DisplayField value={item.Word4} />
</View>
</View>
</TouchableOpacity>
);
};
This is a render code
render() {
return (
<View style={styles.container}>
<SearchBar lightTheme placeholder='Enter your word' onChangeText={this.onChangeTextDelayed} />
<FlatList style={styles.listContainer}
data={this.state.data}
extraData={this.state.data}
renderItem={this.renderItem}
keyExtractor={item => item.Word1}
ItemSeparatorComponent={() => (
<View
style={{ height: 1, width: '100%', backgroundColor: '#CED0CE' }}
/>
)}
/>
</View>
);
}
This is a data retrieving method
retriveData = (msg) => {
if(msg === '') {
this.setState({data:[]});
return;
}
let limStatement = '';
let sqlStatement = "SELECT Word1, Word2, Word3, Word4 FROM MyTable WHERE Word1 LIKE '%" + msg + "%' ORDER BY lower(Word2)";
db.runQuery(sqlStatement).then(rows => { this.setState({ data: rows }) });
}
So when I type for search in textbox Flatlist is showing data properly but when I remove search text it will go to this.setState({data:[]}) to clear state's data out but it happened just some row that not gone,
Example the list show
A
B
C
D
E
F
G
.
.
.
when I clear the search out, it seems like.
B
B
D
D
E
E
F
F
G
G
G
It happens when I scrolling down to far.
Any idea for this?

Related

React Native Image not Rendering

I am building a simple React Native app in which if you search a book, it will find the google books image link stored in a database for that book and map it. However, I am getting an issue. The images aren't rendering on the app, and I've tried multiple solutions to fix this. None of them worked. Please help.
Here is my code:
class Search extends Component {
constructor(props) {
super(props);
this.state = {
search: '',
loading: false,
goBack: false,
results: [],
};
}
render() {
return (
<ScrollView style={{ backgroundColor: '#fff' }}>
<Text></Text>
<Text></Text>
<View style={{ justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ fontSize: 30, marginTop: 20, textAlign: 'center' }}>
Search
</Text>
<TextInput
value={this.state.email}
placeholder="Search For Books"
autoCapitalize="none"
selectionColor="black"
underlineColor="black"
underlineColorAndroid="black"
style={{
marginTop: 50,
fontSize: 20,
width: '75%',
marginBottom: 25,
}}
onChangeText={(e) => {
this.setState({ search: e });
}}></TextInput>
<Button
//icon="login"
mode="outlined"
loading={this.state.loading}
color="black"
onPress={() => {
axios
.post('https://diversitylibrary.herokuapp.com/books/search', {
query: this.state.search,
})
.then((res) => {
console.log(res.data.results.map((obj) => obj));
this.setState({
results: res.data.results.map((obj) => obj),
});
})
.catch((err) => {
console.log(err);
alert(
'Something went wrong. Are you connected to the internet?'
);
});
}}
contentStyle={{
width: 175,
height: 50,
justifyContent: 'center',
alignItems: 'center',
}}>
Search {'->'}
</Button>
<View>
{this.state.results.map((result) => (
<ScrollView>
<Image
key="bookCover"
style={{ width: 200, height: 300 }}
source={{ uri: result.cover }}></Image>
<Text>{result.title}</Text>
</ScrollView>
))}
</View>
</View>
<Text></Text>
<Text></Text>
<Text></Text>
<Text></Text>
</ScrollView>
);
}
}
export default Search;
Thanks. (Let me know if you need to see any other files.)
Try using require like this:
source={require{ uri: result.cover }}

React Native SyntaxError ","

Receiving syntax error, SyntaxError: Unexpected token, expected ",". It is for the third to last line ");"
I am having trouble seeing where I need to insert the ",". I have tried in front of the ); and after. No luck. This is while I was adding the code for google sign in for ios. Would appreciate any assistance.
render() {
LayoutAnimation.easeInEaseOut();
const scrollEnabled = this.state.screenHeight > height;
return (this.state.logedin ?
<View style={styles.container}>
<StatusBar barStyle="light-content"></StatusBar>
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={styles.scrollview}
scrollEnabled={scrollEnabled}
onContentSizeChange={this.onContentSizeChange}
>
<ImageBackground source={require("/Users/carloscraig/NoExcusasRN/screens/assets/grassbcg2.png")}
style={{ width: '100%', height: '100%' }}>
<Image source={require("/Users/carloscraig/NoExcusasRN/screens/assets/noexlogo.png")}
style={styles.logo}>
</Image>
<Text style={styles.greeting}>{'BIENVENIDO!'}</Text>
<View style={styles.errorMessage}>
{this.state.errorMessage && <Text style={styles.error}>{this.state.errorMessage}</Text>}
</View>
<View style={styles.form}>
<View>
<Text style={styles.inputTitle}>correo electrónico</Text>
<TextInput
style={styles.input}
autoCapitalize="none"
onChangeText={email => this.setState({ email })}
value={this.state.email}
></TextInput>
</View>
<View style={{ marginTop: 32 }}>
<Text style={styles.inputTitle}>contraseña</Text>
<TextInput
style={styles.input}
secureTextEntry
autoCapitalize="none"
onChangeText={password => this.setState({ password })}
value={this.state.password}
></TextInput>
</View>
</View>
<TouchableOpacity style={styles.button} onPress={this.handleLogin}>
<Text style={{ color: "#FFF", fontWeight: "500" }}>Iniciar Sesión</Text>
</TouchableOpacity>
<SafeAreaView style={{
flex: 1,
justifyContent: 'center'
}}>
<Image style={{
width: 300,
height:300,
justifyContent: 'center',
alignSelf: 'center'
}} source={{uri: this.state.photo}} />
<Text>{this.state.name}</Text>
<Text>{this.state.email}</Text>
</SafeAreaView>:
<SafeAreaView style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<GoogleSigninButton
style={{ width: 192, height: 48 }}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Light}
onPress={this._signIn}
disabled={this.state.isSigninInProgress} />
</SafeAreaView>
<TouchableOpacity
style={{ alignSelf: "center", marginTop: 32 }}
onPress={() => this.props.navigation.navigate("Register")}
>
<Text style={{ color: "#414959", fontSize: 13 }}>
No tienes una Cuenta? <Text style={{ fontWeight: "500", color: "#E9446A" }}>Regístrate</Text>
</Text>
</TouchableOpacity>
</ImageBackground>
</ScrollView>
</View>
);
}
}
React conditional rendering
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
{isLoggedIn
? <LogoutButton onClick={this.handleLogoutClick} />
: <LoginButton onClick={this.handleLoginClick} />
}
</div>
);
}
You need to add the other half (false case) to the ternary. In this case if you do not want to render anything you need to return null.
render() {
LayoutAnimation.easeInEaseOut();
const scrollEnabled = this.state.screenHeight > height;
return (this.state.logedin ?
<View style={styles.container}>
<StatusBar barStyle="light-content"></StatusBar>
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={styles.scrollview}
scrollEnabled={scrollEnabled}
onContentSizeChange={this.onContentSizeChange}
>
<ImageBackground source={require("/Users/carloscraig/NoExcusasRN/screens/assets/grassbcg2.png")}
style={{ width: '100%', height: '100%' }}>
<Image source={require("/Users/carloscraig/NoExcusasRN/screens/assets/noexlogo.png")}
style={styles.logo}>
</Image>
<Text style={styles.greeting}>{'BIENVENIDO!'}</Text>
<View style={styles.errorMessage}>
{this.state.errorMessage && <Text style={styles.error}>{this.state.errorMessage}</Text>}
</View>
<View style={styles.form}>
<View>
<Text style={styles.inputTitle}>correo electrónico</Text>
<TextInput
style={styles.input}
autoCapitalize="none"
onChangeText={email => this.setState({ email })}
value={this.state.email}
></TextInput>
</View>
<View style={{ marginTop: 32 }}>
<Text style={styles.inputTitle}>contraseña</Text>
<TextInput
style={styles.input}
secureTextEntry
autoCapitalize="none"
onChangeText={password => this.setState({ password })}
value={this.state.password}
></TextInput>
</View>
</View>
<TouchableOpacity style={styles.button} onPress={this.handleLogin}>
<Text style={{ color: "#FFF", fontWeight: "500" }}>Iniciar Sesión</Text>
</TouchableOpacity>
<SafeAreaView style={{
flex: 1,
justifyContent: 'center'
}}>
<Image style={{
width: 300,
height: 300,
justifyContent: 'center',
alignSelf: 'center'
}} source={{ uri: this.state.photo }} />
<Text>{this.state.name}</Text>
<Text>{this.state.email}</Text>
</SafeAreaView>:
<SafeAreaView style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<GoogleSigninButton
style={{ width: 192, height: 48 }}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Light}
onPress={this._signIn}
disabled={this.state.isSigninInProgress} />
</SafeAreaView>
<TouchableOpacity
style={{ alignSelf: "center", marginTop: 32 }}
onPress={() => this.props.navigation.navigate("Register")}
>
<Text style={{ color: "#414959", fontSize: 13 }}>
No tienes una Cuenta? <Text style={{ fontWeight: "500", color: "#E9446A" }}>Regístrate</Text>
</Text>
</TouchableOpacity>
</ImageBackground>
</ScrollView>
</View>
: null);
}

Keyboard not being dismissed with Keyboard.dismiss in React Native

I'm looking to dismiss the keyboard when the user clicks on anywhere outside the keyboard or the input, but the keyboard is not being dismissed when I click elsewhere:
<TouchableWithoutFeedback style={styles.container} onPress={Keyboard.dismiss}>
<View style={styles.inputContainer}>
<TextInput
placeholder="Search Term"
style={styles.input}
onChangeText={setSearch}
value={search}
returnKeyType="search"
onSubmitEditing={handleSubmit}
/>
</View>
</ TouchableWithoutFeedback>
The styling is pretty standard:
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
inputContainer: {
position: 'absolute',
top: stackHeaderHeight + 10,
height: height * .1,
width: '100%',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
You need to change your onPress code of TouchableWithoutFeedback
<TouchableWithoutFeedback style={styles.container} onPress={()=> Keyboard.dismiss()}>
</ TouchableWithoutFeedback>
Sample working code.
render() {
return (
<TouchableWithoutFeedback onPress={()=>Keyboard.dismiss()} >
<View style={{ flex: 1 }}>
<TextInput returnKeyType="search" style={{borderWidth:1,borderColor:'grey',marginTop:100,height:50,marginHorizontal:50}}/>
</View>
</TouchableWithoutFeedback>
);
}
I think you have a problem with your style properties. Please check on your container and inputcontainer style properties. If you have any ScrollView in your render method add following
<ScrollView keyboardShouldPersistTaps='always'>

Error in non-iPhoneX devices - null is not an object

When I run my app on a iPhone X simulator, it works as designed with no errors.
When I run it on an iPhone 5S, 7, and all other non iPhone X devices, I get an error that says, null is not an object (evaluating '_this.props.filters.map)
This is the render part of my code:
render() {
return(
<View>
<NativeText
color='gray'
style={{
paddingLeft: '3%',
paddingTop: '2%',
paddingBottom: '2%',
borderBottomColor: 'gray',
borderBottomWidth: 1,
fontSize: 14
}}>Saved Filters</NativeText>
<View
style={{
borderBottomColor: 'gray',
borderBottomWidth: 1,
}}
/>
<View style={{flexDirection: 'row', flexWrap: 'wrap', backgroundColor: '#f7fafc'}}>
{
this.props.filters !== null ? this.props.filters.map(
(v, i) => {
return(
<View key={i} style={{flexDirection:'row', alignItems:'center', paddingLeft: '2%'}}>
<NativeText
key={i}
onPress={() => this.updateCurrentFilter(i)}
style={{
paddingLeft: 10,
fontWeight: "100",
fontSize: 14
}}
>{v.filter_name} </NativeText>
<View
style={{
alignSelf: 'flex-end'
}}
>
<Icon
name={this.props.checkedFilters !== undefined && this.props.checkedFilters[i] === true ? 'toggle-switch' : 'toggle-switch-off-outline'}
size={29}
style={{top: '7%'}}
color='black'
onPress={() => this.updateCurrentFilter(i)}
/>
</View>
</View>
)
}
) : null
}
</View>
<View
style={{
borderBottomColor: 'gray',
borderBottomWidth: 1,
}}
/>
</View>
)
}
The props comes from a redux reducer with a default state of null.
Is there anything specific to non-iPhone X devices that I should be aware of?
You are getting error because this.props.filters has null value in it
try this :
<View style={{flexDirection: 'row', flexWrap: 'wrap', backgroundColor: '#f7fafc'}}>
{
this.props.filters ?
this.props.filters.length > 0 ?
this.props.filters.map((v, i) => {
return(
</View> // your component here
)})
: null
: null
}
</View>

How can I display more elements in a react native View?

I would like to show on the homepage of my react native app , a view that in turn shows a text element and an icon, then below these 2 I would like to show the amazon logo. I tried but it only shows me the logo.
export default class Header extends Component {
render() {
return (
<View style={{ flex: 1, flexDirection: 'row' }}>
<Text style={styles.Title}>Acquista!</Text>
<Ionicons name="ios-contact" size={42} color='red' style={{ marginTop:
65, marginLeft: 210 }} />
</View> ,
<Logo></Logo>
);
}
}
class Logo extends Component {
render() {
return (
<View>
<Image
style={{ height: 200, width: 200, marginTop: 150 }}
source={require('./assets/images/amazon.jpg')}
/>
</View>
);
}
}
Wrap everything into a View
return (
<View style={{ flex: 1 }}>
<View style={{ flex: 1, flexDirection: 'row' }}>
<Text style={styles.Title}>Acquista!</Text>
<Ionicons name="ios-contact" size={42} color='red' style={{ marginTop: 65, marginLeft: 210 }} />
</View>
<Logo />
</View>
)

Resources