How to scroll flat list without touch effect - react-native

I am new in react native, I have used FlatList which has ListItem as component:
<FlatList
style={styles.container}
data={items}
renderItem={({ item }) => (
<ListItem
title={item.title}
onPress={() => console.log("this is pressed", item.id)}
)}
/>
)}
keyExtractor={(item) => item.id.toString()}
/>
Everything is working fine only the
Problem is: when user scrolls the view i.e FlatList the point where finger is pressed that component shows touch effect. I want touch effect happened only when that element is pressed not when is scrolled. Is there any way to solve this?
Code for ListItem
import React from "react";
import { View, StyleSheet, TouchableHighlight } from "react-native";
import { AppText } from "./AppText";
import colors from "../config/colors";
import Swipeable from "react-native-gesture-handler/Swipeable";
function ListItem({title,onPress}) {
return (
<Swipeable renderRightActions={renderRightActions}>
<TouchableHighlight onPress={onPress}>
<View style={[{ backgroundColor }, styles.container]}>
<View style={styles.textContainer}>
{title && <AppText style={styles.title}>{title}</AppText>}
</View>
</View>
</TouchableHighlight>
</Swipeable>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
padding: 10,
},
textContainer: {
marginLeft: 10,
justifyContent: "center",
},
title: {
fontWeight: "900",
},
});
export default ListItem;

Related

React native button onpress show 2 new buttons and make other stuff unclickable

I want after press on the FloatingButton, to show 2 more buttons.
While I have these 2 buttons shown, I want to make the rest of this page to be uncklickable,
that the user has to press one of these to go on navigating.
This is my code:
import React from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import FloatingButton from '../components/FloatingButton';
const DuellScreen = () => {
return (
<View style={{ flex: 1 }}>
<ScrollView style={styles.container2}>
<Text style={{ flex: 1 }}>body</Text>
</ScrollView>
<View style={styles.container}>
<FloatingButton/>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
padding: 16,
flex: 1,
backgroundColor: '#fff',
justifyContent: 'flex-end',
alignItems: 'center'
},
container2: {
padding: 16,
flex: 5,
backgroundColor: '#fff',
},
});
export default DuellScreen
How can I do this, especially the 2 button visible after buttonpress + make the other things unclickable?
how about this:
import React, { useState } from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import FloatingButton from '../components/FloatingButton';
const DuellScreen = (props) => {
const [active, setActive] = useState(true);
return (
<View style={{ flex: 1 }}>
<ScrollView style={styles.container2}>
<Text style={{ flex: 1 }}>body</Text>
</ScrollView>
<View style={styles.container}>
<FloatingButton onPress={active ? () => setActive(false) : () => {} } />
{!active ?
<View>
<FloatingButton onPress={() => props.navigation.navigate('OtherScreen')} />
<FloatingButton onPress={() => props.navigation.navigate('AnotherScreen')} />
</View>
: null}
</View>
</View>
)
}

How can i select flatlist items and highlights items in react-native using onPress function?

I don't know how to select flatlist items on onPress and display highlight the flatlist items onPress function using flatlist.
In this flatlist i am fettching data using package.js file and using class method
Code
import React from 'react'
import {Stylesheet, View, Text, TouchableOpacity, FlatList, ScrollView } from 'react-native'
import PACKAGE from './Package'
export default class extends React.Component {
constructor(props) {
super(props)
this.state = {
selected: '',
}
_OnPress =() =>{
/**/
}
<FlatList
data={PACKAGE}
contentContainerStyle={{ paddingRight: 20 }}
horizontal
showsHorizontalScrollIndicator={false}
renderItem={({ item, separators }) => (
<View style={{ paddingLeft: 20 }}>
<TouchableOpacity style={[styles.myAccountPriceInfo, { backgroundColor: item.color }]}>
<View>
<Text style={[styles.priceText, { color: item.textColor }]}>{item.name}</Text>
</View>
<View style={styles.priceDetail}>
<Text style={[styles.numText, { color: item.textColor }]}>from </Text>
<Text style={[styles.dollarText, { color: item.textColor }]}>$</Text>
<Text style={[styles.priceNumText, { color: item.textColor }]}>{item.cost}</Text>
</View>
</TouchableOpacity>
</View>
)}
/>
}
You could use the _OnPress function like this.
import React from 'react'
import {Stylesheet, View, Text, TouchableOpacity, FlatList, ScrollView } from 'react-native'
import PACKAGE from './Package'
export default class extends React.Component {
constructor(props) {
super(props)
this.state = {
selected: null,
}
renderItem = ({ item, separators }) => {
// you can know here whether the item is selected or not.
const isSelected = item.name === this.selected.selectedItem;
return (
<View style={{ paddingLeft: 20 }}>
<TouchableOpacity
style={[
styles.myAccountPriceInfo, { backgroundColor: item.color },
// you could highlight your selected item like this.
isSelected && styles.highlightSelected
]}
// let the state know which item has been selected
onPress={() => this.setState(item.name)}
>
<View>
<Text style={[styles.priceText, { color: item.textColor }]}>{item.name}</Text>
</View>
<View style={styles.priceDetail}>
<Text style={[styles.numText, { color: item.textColor }]}>from </Text>
<Text style={[styles.dollarText, { color: item.textColor }]}>$</Text>
<Text style={[styles.priceNumText, { color: item.textColor }]}>{item.cost}</Text>
</View>
</TouchableOpacity>
</View>
)
}
return (
<FlatList
data={PACKAGE}
contentContainerStyle={{ paddingRight: 20 }}
horizontal
showsHorizontalScrollIndicator={false}
renderItem={renderItem}
/>
)
}

React Native - Close modal when click out of it on IOS

I'm new to React Native. I've added a modal to my app and I want it to be closed when I click outside the modal. But nothing happens when I click out of the modal.
Here is my code
import React from 'react';
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { Button } from 'react-native-elements';
import Modal from 'react-native-modal';
import { style } from './style';
const MenuTask = ({ isVisible, onDisapearCallBack }) => (
<TouchableWithoutFeedback onPress={() => onDisapearCallBack()}>
<Modal
isVisible={isVisible}
animationIn={'zoomInDown'}
animationOut={'zoomOutUp'}
animationInTiming={1000}
animationOutTiming={1000}
backdropTransitionInTiming={1000}
backdropTransitionOutTiming={1000}
>
<TouchableWithoutFeedback>
<View style={style.modal}>
<View style={style.textView}>
<Text style={style.modalTitle}>Que souhaitez vous faire sur la tâche ?</Text>
</View>
<View style={style.buttonView}>
<Button
buttonStyle={style.buttonDelete}
title = "Supprimer"
onPress={() => onDisapearCallBack()}
/>
<Button
buttonStyle={style.buttonChangeStatus}
title = "Changer status"
onPress={() => onDisapearCallBack()}
/>
</View>
</View>
</TouchableWithoutFeedback>
</Modal>
</TouchableWithoutFeedback>
);
export default MenuTask;
Please could you help me to figure this out. Thanks a lot :)
#ramashish tomar thanks for your help. I tried to apply what you said but still not working :(
Here is my code
import React from 'react';
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { Button } from 'react-native-elements';
import Modal from 'react-native-modal';
import { style } from './style';
const MenuTask = ({ isVisible, onDisapearCallBack }) => (
<View>
<Modal
isVisible={isVisible}
animationIn={'zoomInDown'}
animationOut={'zoomOutUp'}
animationInTiming={1000}
animationOutTiming={1000}
backdropTransitionInTiming={1000}
backdropTransitionOutTiming={1000}
>
<TouchableWithoutFeedback onPress={() => onDisapearCallBack()}>
<View style={style.modal}>
<TouchableWithoutFeedback>
<View>
<View style={style.textView}>
<Text style={style.modalTitle}>Que souhaitez vous faire sur la tâche ?</Text>
</View>
<View style={style.buttonView}>
<Button
buttonStyle={style.buttonDelete}
title = "Supprimer"
onPress={() => onDisapearCallBack()}
/>
<Button
buttonStyle={style.buttonChangeStatus}
title = "Changer status"
onPress={() => onDisapearCallBack()}
/>
</View>
</View>
</TouchableWithoutFeedback>
</View>
</TouchableWithoutFeedback>
</Modal>
</View>
);
export default MenuTask;
I can close the modal by clicking on it or on both buttons but not outside it. Really weird.
Here is the screenshot of the modal:
Modal screenshot
Maybe you could help me
Thanks in advance
You don't need TouchableWithoutFeedback outside the modal as by default modal covers the whole screen.
You can try something like this-
import React, { useState } from "react";
import {
View,
Text,
TouchableWithoutFeedback,
StyleSheet,
Button,
Modal,
TouchableOpacity
} from "react-native";
function MenuTask() {
const [isVisible, onDisapearCallBack] = useState(false);
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "orange"
}}
>
<Modal animationType="fade" transparent={true} visible={isVisible}>
<TouchableWithoutFeedback onPress={() => onDisapearCallBack(false)}>
<View style={{ flex: 1, backgroundColor: "rgba(0,0,0,0.7)" }}>
<TouchableWithoutFeedback>
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "white",
marginVertical: 150,
marginHorizontal: 10
}}
>
<Text>Modal Content</Text>
</View>
</TouchableWithoutFeedback>
</View>
</TouchableWithoutFeedback>
</Modal>
<Text style={{ color: "white", fontSize: 30 }}>Its page content</Text>
<TouchableOpacity
onPress={() => onDisapearCallBack(true)}
style={{
backgroundColor: "red",
borderRadius: 10,
paddingHorizontal: 30,
paddingVertical: 10,
marginTop: 20
}}
>
<Text style={{ color: "white", fontSize: 18 }}>Open Modal</Text>
</TouchableOpacity>
</View>
);
}
export default MenuTask;

Why does FlatList's renderItem prop return inconsistent data from array?

Goal: Use react-native's FlatList to render a list of touchable image's that when pressed, display a Modal of that respective image.
List.js:
import React, { useState } from 'react';
import {
View,
Text,
Image,
StyleSheet,
Modal,
FlatList,
Dimensions,
TouchableOpacity,
TouchableWithoutFeedback
} from 'react-native';
const { height, width } = Dimensions.get('window');
const List = (props) => {
const [ visible, setVisible ] = useState(false);
return (
<View style={styles.container}>
<FlatList
data={props.data}
numColumns={2}
renderItem={({ item, index }) => {
return (
<View>
<TouchableOpacity onPress={() => setVisible(true)} style={styles.itemContainer}>
<Image style={styles.img} resizeMode={'contain'} source={item.img} />
<View style={styles.details}>
<Text style={{ color: 'white', fontWeight: 'bold' }}>{item.name}</Text>
<Text style={{ color: 'white' }}>{item.date}</Text>
</View>
</TouchableOpacity>
<Modal
animationType="fade"
transparent={false}
visible={visible}
presentationStyle={'overFullScreen'}
>
<View style={styles.modalContainer}>
<TouchableWithoutFeedback onPress={() => setVisible(false)}>
<Image resizeMode={'contain'} style={styles.modalImg} source={item.img} />
</TouchableWithoutFeedback>
</View>
</Modal>
</View>
);
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center'
},
img: {
height: width * 0.65,
width: width * 0.45
},
modalImg: {
left: width * 0.12,
top: height * 0.2
},
modalContainer: {
flex: 1,
backgroundColor: 'rgba(69, 74, 102, .7)'
},
details: {
position: 'absolute',
backgroundColor: 'rgba(137,137,137, 0.75)',
borderRadius: 10,
height: width * 0.125,
width: width * 0.4,
left: '4%',
top: '75%',
justifyContent: 'center',
alignItems: 'center'
}
});
export default List;
Implementation:
import React from 'react';
import { StyleSheet, Dimensions, SafeAreaView } from 'react-native';
import Component from './Component';
const { width } = Dimensions.get('window');
const testArr = [
{ name: 'Josh Gordon', date: 'November 7, 2019', img: require('./src/assets/img/josh.png') },
{ name: 'Kylie Jenner', date: 'December 21 2019', img: require('./src/assets/img/kylie.png') },
{ name: 'Logic', date: 'December 15, 2019', img: require('./src/assets/img/logic.png') }
];
const App = () => {
return (
<SafeAreaView style={styles.screen}>
<Component data={testArr} />
</SafeAreaView>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
backgroundColor: '#E6E6E6'
}
});
export default App;
Problem: The initial FlatList renders with all three different images, but when any of the image buttons are pressed, the model is displayed with only the last image from the data array, even though the same item.img variable is used... Why?
You have as many modals as there are items in the list, but only one visible variable which all modals use. When you click any item, visible becomes true for all modals and they are all rendered at once so you only see one of them.
Don't put modals inside the FlatList renderItem. Modal should always be rendered in the dom (but not always visible). Make only one modal, and store related information (modal visibility and which item to display inside modal) in the state. Then change it in onPress of your TouchableOpacity

How to make button with background image redirect to WebView React Native

I have problem to make button with background image and then redirect to my WebView React Native.
I already have run code to show my web view, and I want before my web view show, its triggered from button click who look like this
Below is the code for my WebView;
import React, { Component } from 'react'
import {
View,StyleSheet,AppRegistry
} from 'react-native'
import {WebView} from 'react-native-webview'
export default class ActivityIndicatorDemo extends Component {
render() {
return (
<View style = {styles.container}>
{/*<WebView
source={{html: '<h1>Hello javaTpoint</h1>'}}
/>*/}
{/* <WebView
source={require("./resources/index.html")}
/>*/}
<WebView
// injectedJavaScript={false}
// javaScriptEnabled={false}
// originWhitelist={['*']}
source = {{ uri:'http://myip/login' }}
/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
flexDirection: 'column',
flex: 1,
}
})
AppRegistry.registerComponent('App', () => ActivityIndicatorDemo)
How I can implement button with background image before my WebView shows?
Here is a simple way to do it.
import React, {useState} from 'react';
import {
View,
ImageBackground,
TouchableOpacity,
Text,
StatusBar,
} from 'react-native';
import WebView from "react-native-webview";
const App = () => {
const [visible, showWebview] = useState(false)
const setVisible = (visible) => {
showWebview(visible);
}
return (
<View
style={{
width:'100%',
height:"100%"
}}
>
{
!visible? <View
style={{
width:'100%',
height:"100%"
}}
> <ImageBackground
source={{uri:"https://images.unsplash.com/photo-1556742031-c6961e8560b0?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&dl=clay-banks-tgquMvhNLc4-unsplash.jpg"}}
style={{width:"100%",height:"90%",zIndex:999,justifyContent:"center",alignItems:"center"}}
/> <TouchableOpacity
onPress={() => setVisible(true)}
style={{
backgroundColor:"white",
justifyContent:"center",
alignItems:"center",
padding:10,height:"10%"
}}
>
<Text>Show Webview</Text>
</TouchableOpacity> </View> :
<View
style={{
flex:1
}}
>
<TouchableOpacity
onPress={() => setVisible(false)}
style={{
backgroundColor:"red",
padding:10,
justifyContent:'center',
alignItems:'center',
width:"100%"
}}
>
<Text style={{color:'white'}} >Hide Webview</Text>
</TouchableOpacity>
<WebView
scalesPageToFit
originWhitelist={["*"]}
source={{ uri:"https://www.google.com"
}}/>
</View>
}
</View>
);
};
export default App;