How can i display the following output in react native snap carousel - reactjs

How can i display the following output in react native snap carousel
<Carousel
autoplayInterval={4000}
// slideStyle={{ flex: 1,
// marginRight:10}}
ref={ (c) => { this._carousel = c; } }
data={this.state.featuredBusinessData}
scrollInterpolator={stackScrollInterpolator}
slideInterpolatedStyle={stackAnimatedStyles}
useScrollView={true}
renderItem={this._renderItemFeaturedBusiness.bind(this)}
onSnapToItem={this.handleSnapToItem.bind(this)}
sliderWidth={deviceWidth}
// itemWidth={deviceWidth/2.5 }
itemWidth={deviceWidth/3 }
layout={'default'}
// loopClonesPerSide={this.state.videos.length-1}
firstItem={2}
autoplay={true}
loop={true}
useScrollView={true}
enableMomentum={true}
lockScrollWhileSnapping={false}
/>
as I'm new to RN can please anybody tell me how can i display the following output in react native snap carousel

Output:
#Waheed Akhtar gave you the perfect suggestion, but if you still have some difficulties in implementation here is the full working example:
import * as React from 'react';
import { Text, View, StyleSheet, FlatList, Image } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default function App() {
const videos = [
{
id: 'WpIAc9by5iU',
thumbnail: 'https://source.unsplash.com/random',
title: 'Led Zeppelin - Stairway To Heaven',
},
{
id: 'sNPnbI1arSE',
thumbnail: 'https://img.youtube.com/vi/sNPnbI1arSE/hqdefault.jpg',
title: 'Eminem - My Name Is',
},
{
id: 'VOgFZfRVaww',
thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
title: 'some title',
},{
id: 'WpIAc9by5iU',
thumbnail: 'https://source.unsplash.com/random',
title: 'Led Zeppelin - Stairway To Heaven',
},
{
id: 'sNPnbI1arSE',
thumbnail: 'https://source.unsplash.com/random',
title: 'Eminem - My Name Is',
},
{
id: 'VOgFZfRVaww',
thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
title: 'some title',
},
];
return (
<View style={styles.container}>
<FlatList
horizontal
data={videos}
renderItem={({ item }) => (
<View style={styles.card_template}>
<Image
style={styles.card_image}
source={{
uri:
item.thumbnail,
}}
/>
<View style={styles.text_container}>
<Text style={styles.card_title}>{item.title.toUpperCase().substr(0, 10)+"..."}</Text>
<Text style={styles.card_subtitle}>{item.id}</Text>
</View>
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
card_template: {
width: 150,
height: 150,
marginLeft: 10,
marginTop: 20,
boxShadow: '20px 20px 17px -12px rgba(0,0,0,0.75)',
borderRadius: 10,
},
card_image: {
width: 150,
height: 150,
borderRadius: 10,
},
text_container: {
position: 'absolute',
width: 150,
height: 50,
bottom: 0,
paddingVertical: 2,
backgroundColor: 'rgba(255,255,255, 1)',
borderBottomLeftRadius: 10,
borderBottomRightRadius: 10,
},
card_title: {
color: 'black',
fontSize: 14,
marginLeft: 10,
},
card_subtitle:{
color: 'grey',
fontSize: 14,
marginLeft: 10,
}
});
You can play with working code here: Expo Snack

Use Scrollview or Flatlist to render the above ListItems with horizontal={true} props
https://reactnative.dev/docs/scrollview#horizontal

Related

How to disable the Touchable Opacity for some specific item that comes from API in React Native

I have an API and I need to set the Touchable Opacity separately
for each item. I have done it already. but now I need to disable some
Touchable Opacity. Like what I am trying to do is if Status and Check in Api both are True then the User can move to next and the color of it is red.
screen by pressing on that touchable opacity but if one of them is
false then the touchable opacity will be disabled and a user can't move to the next screen and the color of it will be grey I don't know how to do it because I am very new in this kind of functionality in React-native I read different questions and answers but unfortunately that doesn't help me.
API Response
const results = [
{
Id: "IySO9wUrt8",
Name: "Los Stand",
Category: "Mexican",
Status: true,
Check: true,
},
{
Id: "IySO9wUrt8",
Name: "Los Stand 2",
Category: "Burger",
Status: true,
Check: true,
},
{
Id: "IySO9wUrt8",
Name: "Los Stand 3",
Category: "BBq",
Status: true,
Check: true,
},
];
My full code
App.js
import React, { useEffect, useState } from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
FlatList,
SafeAreaView,
} from 'react-native';
import { Card, TextInput, RadioButton } from 'react-native-paper';
const results = [
{
Id: 'IySO9wUrt8',
Name: 'Los Stand',
Category: 'Mexican',
Status: true,
Check: true,
},
{
Id: 'IySO9wUrt8',
Name: 'Los Stand 2',
Category: 'Burger',
Status: true,
Check: true,
},
{
Id: 'IySO9wUrt8',
Name: 'Los Stand 3',
Category: 'BBq',
Status: true,
Check: true,
},
];
export default function App() {
const renderItem = ({ item }) => {
return (
<>
<TouchableOpacity
onPress={() =>
navigation.navigate('NextScreen', {
name: item.Name,
phone: item.Phone,
})
}>
<View
style={{
flexDirection: 'column',
marginHorizontal: 10,
marginVertical: 10,
padding: 5,
backgroundColor: 'grey',
borderRadius: 15,
}}>
<View
style={{
flexDirection: 'column',
alignSelf: 'flex-start',
marginTop: 10,
marginBottom: 10,
}}>
<Text
style={{
fontSize: 15,
fontWeight: '900',
color: '#424242',
}}>
{item.Name}
</Text>
<Text>{item.Phone}</Text>
</View>
</View>
</TouchableOpacity>
</>
);
};
return (
<SafeAreaView style={styles.container}>
<FlatList
style={styles.container}
data={results}
renderItem={renderItem}
keyExtractor={(item, index) => index.toString()}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 2,
backgroundColor: 'white',
},
});
AssetExample.js
import * as React from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
export default function AssetExample() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Local files and assets can be imported by dragging and dropping them into the editor
</Text>
<Image style={styles.logo} source={require('../assets/snack-icon.png')} />
</View>
);
}
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
padding: 24,
},
paragraph: {
margin: 24,
marginTop: 0,
fontSize: 14,
fontWeight: 'bold',
textAlign: 'center',
},
logo: {
height: 128,
width: 128,
}
});
You can edit code in Expo Snack
My Expo code for live editing
TouchableOpacity has a disabled property, so you should use it. It will prevent onPress from executing and navigating the user.
As for the styling, you can check the style using the same login.
<TouchableOpacity disabled={!result.Status || !result.Check} style={{ background: (!result.Status || !result.Check) ? 'grey' : 'red' }}>

How can we create Side Tabs in React Native

Creating Side Menu in React Application.
This would work both with Expo and react
Create a Navigation Service(Navigation-service.ts)
import { NavigationActions } from 'react-navigation';
function navigate(routeName: any, params?: any) {
NavigationActions.navigate({
routeName,
params
});
setRoute(routeName);
}
export default {
navigate
}
After this
create a new file sidemnu.tsx
import NavigationService from Navigation-service.ts;
import { View, TouchableHighlight, Image } from 'react-native';
openHome() {
NavigationService.navigate('Home');
}
render(){
return (
<View style={[this.props.isLoggedIn ? styles.container : styles.containerHidden]}>
<View style={[this.props.isLoggedIn ? styles.tabContainer : styles.tabContainerHidden]}>
<View>
<TouchableHighlight
style={this.props.currentTab === 'Home' ? styles.activeTab : styles.menuBtnContainer}
onPress={() => this.openHome()}
disabled={!this.props.isLoggedIn}
underlayColor='red'
>
<Image
source='any image url'
/>
</TouchableHighlight>
</View>
</View>
</View>
);
}
Add style
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
width: 108,
backgroundColor: '#002A5C',
borderTopColor: '#002457',
borderWidth: 1
},
containerHidden: {
width: 108,
backgroundColor: '#002A5C'
},
tabContainer: {
flex: 1,
flexDirection: 'column'
},
tabContainerHidden: {
flex: 1,
display: 'none'
},
activeTab: {
backgroundColor: '#008cc3',
height: 108,
width: 108,
padding: 10,
alignSelf: 'center',
justifyContent: 'center'
},
menuBtnContainer: {
height: 80,
width: 80,
alignSelf: 'center',
justifyContent: 'center'
},
});
Call in app.tsx file
<Sidemnu />
Whenever you want to display this side menu.
If you have any queries, please write back.

Horizontal scrollview inside another horizontalscroll view in react-native

I'm designing UI in react native where I've a HorizontalScrollView inside another HorizontalScrollView. However, the child HorizontalScrollView doesn't work.
Is it possible to disable the parent HorizontalScrollView whenever a gesture is made in child HorizontalScrollView. I know this sounds tricky but I need to implement this in my UI.
note: working fine in IOS
code snippet:
import React from 'react';
import { StyleSheet, Text, View, Dimensions, SafeAreaView, TouchableHighlight, ScrollView,
TouchableOpacity, TouchableWithoutFeedback, Image, Linking, Platform } from 'react-native';
import Carousel, { Pagination } from 'react-native-snap-carousel';
import { Input, Button, Divider, Card, Overlay } from 'react-native-elements';
export default class Cart extends React.Component {
state = {
data: [
{ title: 'a', url: require("./Light-Hover34x34.png") },
{ title: 'b', url: require("./Light-Hover34x34.png") },
{ title: 'c', url: require("./Light-Hover34x34.png") },
{ title: 'd', url: require("./Light-Hover34x34.png") },
{ title: 'e', url: require("./Light-Hover34x34.png") },
],
activeSlide: 0,
room: ["one", "two", "three"]
}
_renderItem = ({ item, index }) => {
console.log("active slide", this.state.activeSlide)
const isActive = index === this.state.activeSlide;
return (
<TouchableHighlight
// onPress={() => Linking.openURL(item.url)}
style={{
backgroundColor: isActive ? '#FFD845' : null,
width: 60,
height: 60,
borderRadius: 60/2,
justifyContent: 'center',
alignItems: 'center',
}}>
<Image
source={item.url}
style={{ width: '50%', height: '40%' }}
/>
</TouchableHighlight>
);
};
render() {
return (
<View style={{alignItems: 'center', justifyContent: 'center', paddingVertical: 100}}>
<ScrollView horizontal={true}>
{
this.state.room.map((r, i) => {
return(
<View key={i}>
<TouchableWithoutFeedback>
<Card containerStyle={[styles.cardEleBig,]}>
<SafeAreaView style={{ height: 150 }}>
<Carousel
data={this.state.data}
renderItem={this._renderItem}
sliderWidth={120}
itemWidth={50}
onSnapToItem={index => this.setState({ activeSlide: index })} //for pagination
/>
</SafeAreaView>
</Card>
</TouchableWithoutFeedback>
</View>
)
})
}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
cardCircleParking: {
backgroundColor: "yellow",
// marginBottom: 10,
// marginLeft: '5%',
width: 50,
height: 50,
borderRadius: 50/2,
borderColor: 'white',
},
cardEleBig: {
borderRadius: 20,
borderBottomWidth: 0,
borderColor: 'white',
width: 159,
height: Platform == "ios" ? 210 : 200,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
elevation: 5,
marginLeft: 5,
marginRight: 5,
marginTop: 10,
marginBottom: 2
},
})
But this code is working fine with IOS
Thanks in advance

Is it possible to drag item outside the scope of horizontal FlatList?

I am working on the FotoBook application using React Native library. After i let the user to choose images from device library i put them into FlatList component in horizontal mode. So how is it possible to drag and drop selected image on the other part of the screen
I am using PanResponder and Animated API and it transforms responders coordinates well, but when I try to exceed the scope of horizontal FlatList it is getting hidden. I tried to set overflow to visible and change zIndex but it does not work
Here are the screenshots:
Here is my code:
import React from 'react';
import {
SafeAreaView,
View,
FlatList,
Text,
StyleSheet,
Animated,
PanResponder
} from 'react-native';
import { v4 } from 'uuid';
export default class App extends React.PureComponent{
state = {
data: [
{ id: v4(), title: 'Lightcoral', hex: '#eb7474' },
{ id: v4(), title: 'Orchid', hex: '#eb74dc' },
{ id: v4(), title: 'Mediumpurple', hex: '#9a74eb' },
{ id: v4(), title: 'Mediumslateblue', hex: '#8274eb' },
{ id: v4(), title: 'Skyblue', hex: '#74b6eb' },
{ id: v4(), title: 'Paleturquoise', hex: '#93ece2' },
{ id: v4(), title: 'Palegreen', hex: '#93ecb6' },
{ id: v4(), title: 'Khaki', hex: '#d3ec93' }
]
}
_position = new Animated.ValueXY()
_panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (event, gesture) => {
this._position.setValue({ x: gesture.dx, y: gesture.dy})
},
onPanResponderRelease: () => {}
})
_keyExtractor = (item) => item.id;
_renderItem = ({item}) => {
return (
<Animated.View
style={this._position.getLayout()}
{...this._panResponder.panHandlers}
>
<View style={[styles.itemBox, {backgroundColor: `${item.hex}`}]}>
<Text>{item.title}</Text>
</View>
</Animated.View>
)
}
render() {
const { data } = this.state
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
<View style={styles.targetArea}>
<Text>Drop HERE!!!</Text>
</View>
<View style={{ height: 80, borderColor: 'black', borderWidth: 2 }}>
<FlatList
data={data}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
horizontal={true}
/>
</View>
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
safeArea: {
flex: 1
},
container: {
flex: 1,
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: '#fff',
},
itemBox: {
width: 80,
height: 80,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1,
borderColor: '#fff'
},
targetArea: {
height: 150,
width: 150,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1,
borderColor: '#eee',
backgroundColor: '#F5FCFF',
marginTop: 40
}
});

FlatList in React Native does not display any content

I just learned that there is a flatList in React Native. I have a list of services, when the user tap on each service, they are redirected to the address where those services are available. I am trying to display these addresses in FlatList. The screen shot of the list of services is below:
when the user taps on Test service, I want to show the address in the form of FlatList. I am not sure, but whatever I am displaying inside flatList is not showing on the page. Below is my entire code including flatlist:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import { Text, View, StyleSheet, Image, ScrollView, TouchableOpacity, Linking, Button, FlatList } from 'react-native';
import { connect } from 'react-redux';
import { getTheme } from 'react-native-material-kit';
import EvilIcon from 'react-native-vector-icons/EvilIcons';
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
import SimpleIcon from 'react-native-vector-icons/SimpleLineIcons';
import * as actions from '../actions';
import getDirections from 'react-native-google-maps-directions'
const theme = getTheme();
const styles = StyleSheet.create({
card: {
marginTop: 10,
paddingBottom: 20,
marginBottom: 20,
borderColor: 'lightgrey',
borderWidth: 0.5,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#4F6D7A',
height: 500,
alignSelf:'center',
width:500,
position: 'relative',
marginTop: 5,
top: 10
},
title1: {
top: 10,
left: 80,
fontSize: 24,
},
title2: {
top: 35,
left: 82,
fontSize: 18,
},
image: {
flex: 0,
height: 100,
width: 333,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
},
closeIcon: {
position: 'absolute',
top: 5,
left: 295,
color: 'black'
},
icon: {
position: 'absolute',
top: 15,
left: 0
},
textArea: {
flexDirection: 'row',
paddingLeft: 20,
paddingTop: 10,
width: 260,
},
textIcons: {
color: '#26a69a',
},
actionArea: {
paddingTop: 10,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
},
title:{
justifyContent: 'center',
paddingTop: 10,
alignItems: 'center',
alignSelf: 'center',
fontWeight: 'bold',
fontSize: 22,
color: 'black'
},
SerContent:{
fontWeight: 'bold',
fontSize: 16,
paddingTop: 10,
alignSelf: 'center',
color: 'black'
},
underLineText: {
fontSize: 17,
textDecorationLine: 'underline',
color: 'black',
fontWeight: 'bold',
alignSelf: 'center'
},
dir:{
flexDirection:'row',
paddingTop: 30,
alignSelf: 'center',
} ,
dirAddr:{
flexDirection:'column',
paddingTop: 30,
alignSelf: 'center'
},
Address1:{
alignSelf: 'center',
marginRight: 20,
fontSize: 17,
fontWeight: 'bold',
color: 'black'
},
fullAddress:{
marginRight: 20,
fontWeight: 'bold',
color: 'black'
},
toolbar:{
flexDirection:'row' , //Step 1
},
toolbarTitle:{
width: 10,
top: 0,
left: 0,
bottom: 0,
right: 0,
flex:1 //Step 3
},
buttonShape:{
margin: 40,
width:160,
marginLeft: 80,
backgroundColor:'#00BCD4',
},
producetBox:{
margin:10,
fontWeight:'bold',
color:'#000',
alignSelf:'center'
},
price:{
padding:5,
margin:10,
borderColor:'orange',
borderBottomWidth: StyleSheet.hairlineWidth,
}
});
class ServiceDetail extends Component {
handleClick = (link) => {
Linking.canOpenURL(link).then(suppported => {
if (supported) {
Linking.openURL(link);
} else {
console.log('Don\'t know how to open URI: ' + link);
}
});
};
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
error: null,
};
}
componentDidMount()
{
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
}
render() {
var online="";
var destUrl = 'https://www.google.com/maps/dir/Current+Location/' + this.props.services.destAddr1 ;
var destUrl1 ='https://www.google.com/maps/dir/Current+Location/' + this.props.services.destAddr2 ;
var Online= this.props.services.onlineURL;
return (
<View>
<View style={styles.toolbar}>
<Image
resizeMode='contain'
style={styles.toolbarTitle}
source={require('../Resources/AcrLogoWithDesc.jpg')} />
</View>
<View>
<Text style={styles.title}>{this.props.services.ser}</Text>
<Text style={styles.SerContent} >Service is available in the following locations:</Text>
</View>
<View>
<FlatList
data={this.props.services}
renderItem={({item})=>
<View>
<Text>{item.ser}</Text>
</View>
}
/>
</View>
<View style={styles.buttonShape}>
<Button onPress={() => this.props.noneSelected()} title = 'Close'/>
</View>
</View>
);
}
}
const mapStateToProps = state => {
return {
services: state.serviceSelected
};
};
export default connect(mapStateToProps, actions)(ServiceDetail);
I can see the content of {this.props.services.ser}, but I cannot see the content of <Text>{item.services.ser}</Text> which is inside the flatList. below is the screen shot of the service details when the user taps on Test service:
Item.services.serv content is not appearing on my second screen shot. Below is my JSON and the screen shot of resulting screen file":
[
{
"id":0,
"ser": "Test Service",
"Location": "TestLoc",
"Phone1":"(999)-999-5050",
"SecondLoc": "TestLoc2",
"email":"test#test.com",
"sourceLat":"33.977806",
"sourceLong":"-117.373261",
"destLatL1":"33.613355",
"destLongL1":"-114.596569",
"destLatL2":"33.761693",
"destLongL2":"-116.971169",
"destAddr1": "Test Address, Test Drive",
"destAddr2": "Test Address2, Test Drive2",
"onlineURL":"",
"Phone2": "(900)-900-3333"
},
]
[1]: https://i.stack.imgur.com/xiZMp.png
Any help will be highly appreciated. I tried below things for the flatList
<View>
<FlatList
data={this.props.services}
keyExtractor={(item, index) => item.services.id}
renderItem={({item})=>
<View>
<Text>{item.ser}</Text>
</View>
}
/>
</View>
This is another way that I tried:
<View>
<FlatList
style={{}}
data={this.props.services}
keyExtractor={(item, index) => item.key}
renderItem={(rowData) =>this.RenderFeedCard(rowData)}
/>
</View>
I assume that the JSON that you provide is for this.props.services. So the call inside renderItem={({item})=> {item.services.ser} should be {item.ser} instead.
The reason why is because Flatlist 'data' takes an array (eg: this.props.services) and 'renderItem' will take each item (this.props.services[0]) of the array and do something about it. So you only have to write {item.ser} in order to get the string.
renderItem={({item})=>(
<View>
<Text>{item.ser}</Text>
</View>
)}

Resources