Hi I hope to find the answer here, this is my code I used a list item and the navigation worked but all the items go to one screen I want to know how to make each one to go to a different screen and I didn't know how to do it I am new in React native programming
import React, { Component } from 'react'; import {Image , StyleSheet,
View, Text, Button } from 'react-native';
import { ListItem, Icon }from 'react-native-elements';
import { StackNavigator } from 'react-navigation';
import call from 'react-native-phone-call';
class First extends Component {
render() { let pic = {
uri: 'https://www.lscoiffure.fr/images/assistance.jpg' };
return (
<View style={styles.container}>
<View>
<Image source={pic} style={{width: 350, height: 200}}
/>
<View style={{position: 'absolute', left: 0, right: 0, bottom: 0, justifyContent: 'center',marginBottom:20, alignItems: 'center'}}>
<Text style={{color :'#ffffff',fontSize:24}}>Assistance</Text> </View> </View>
{
list.map((item, i) => (
<ListItem
key={i}
title={item.title}
titleStyle={{ color: 'black' }}
chevronColor="black"
leftIcon={{ name: item.icon ,color:'black'}}
onPress={() => this.props.navigation.navigate('HomeScreen')}
/>
))
}
</View>
);
}
}
const list = [
{
title: 'Appeler le service clientèle',
icon: 'perm-phone-msg',
},
{
title: 'FAQ',
icon: 'help',
}, {
title: 'Conditions et mentions légal',
icon :'error',
},
]
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:'#ffffff'
},
})
export default FirstActivity;
I want to know how to use onPress with condition or any solution you suggest.
Change your list, so that every item in your list contains the page you want to navigate like i do below:
const list = [{
title: 'Appeler le service clientèle',
icon: 'perm-phone-msg',
page: 'HomeScreen'
},
{
title: 'FAQ',
icon: 'help',
page: 'OtherScreen'
}, {
title: 'Conditions et mentions légal',
icon: 'error',
page: 'OtherOtherScreen'
},
]
And then in your navigate method use this page:
onPress={() => this.props.navigation.navigate(item.page)}
Related
unfortunately I am struggling to really grip how this library works, specifically the ParallaxImage component. I was wondering if someone could help me out. I am struggling to get my images to render.
Here are the docs: https://www.npmjs.com/package/react-native-snap-carousel#props-methods-and-getters
here is the following code but I also have a snack already running it here
It doesn't load on the web btw, you have to choose IOS or android.
import React, {useRef, useState, useEffect} from 'react';
import Carousel, {ParallaxImage} from 'react-native-snap-carousel';
import {
View,
Text,
Dimensions,
StyleSheet,
TouchableOpacity,
Platform,
} from 'react-native';
const ENTRIES1 = [
{
title: 'Text',
thumbnail: require('./assets/splash.png'),
},
{
title: 'Text 1',
thumbnail: require('./assets/splash.png'),
},
{
title: 'Text 2',
thumbnail: require('./assets/splash.png'),
},
];
const {width: screenWidth} = Dimensions.get('window');
const MyCarousel = props => {
const [entries, setEntries] = useState([]);
const carouselRef = useRef(null);
const goForward = () => {
carouselRef.current.snapToNext();
};
useEffect(() => {
setEntries(ENTRIES1);
}, []);
const renderItem = ({item, index}, parallaxProps) => {
return (
<View style={styles.item}>
<ParallaxImage
source={{uri: item.thumbnail}}
containerStyle={styles.imageContainer}
style={styles.image}
parallaxFactor={0.4}
{...parallaxProps}
/>
<Text style={styles.title} numberOfLines={2}>
{item.title}
</Text>
</View>
);
};
return (
<View style={styles.container}>
<Carousel
ref={carouselRef}
sliderWidth={screenWidth}
sliderHeight={screenWidth}
itemWidth={screenWidth - 60}
data={entries}
renderItem={renderItem}
hasParallaxImages={true}
/>
</View>
);
};
export default MyCarousel;
const styles = StyleSheet.create({
container: {
flex: 1,
},
item: {
width: screenWidth - 60,
height: screenWidth - 60,
},
imageContainer: {
flex: 1,
marginBottom: Platform.select({ios: 0, android: 1}), // Prevent a random Android rendering issue
backgroundColor: 'white',
borderRadius: 8,
},
image: {
...StyleSheet.absoluteFillObject,
resizeMode: 'cover',
},
});
I think you need to replace source={{uri: item.thumbnail}} with source={item.thumbnail}
because uri is used for web image url like https://image.png and require is used for the local image. I am tested on my android it's working fine now
As I indicated in the Picture with arrow mark, I would like to get the top positions of FlatList. I tried onLayout props, But it return only null values.
Is there anyway to get the top positions of FlatList?
Check below sample
import React, {Component} from 'react';
import {View, FlatList, StyleSheet, Text} from 'react-native';
const 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',
},
];
export default class HomeScreen extends Component {
findDimesions = layout => {
const {x, y, width, height} = layout;
console.log(x);
console.log(y);
console.log(width);
console.log(height);
};
render() {
return (
<View
onLayout={event => this.findDimesions(event.nativeEvent.layout)}
style={styles.container}>
<FlatList
data={DATA}
renderItem={({item}) => (
<View style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</View>
)}
keyExtractor={item => item.id}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
item: {
backgroundColor: '#f9c2ff',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
},
title: {
fontSize: 32,
},
});
Change this according to your requirements.
Hope this helps you. Feel free for doubts.
return (
<View style={styles.container}>
<SafeAreaView style={styles.top} />
<FlatList
...
/>
</View>
);
}
const styles = StyleSheet.create({
top: { flex: 0 },
}
I am working on a React Native app, what I was trying is to create a Custom ListView Item just like android where I can use TextViews, ImageView etc as a placeholder. The thing is I didn't find anything yet that could help me.
Desired Output
Listview is deprecated. Do try using Flatlist for the same, Do check out my code in expo snack expo-snack wher it resembles some part, exact design you can make of your own,
below is the code :
import React from 'react';
import { SafeAreaView, View, FlatList, StyleSheet, Text,Image } from 'react-native';
import Constants from 'expo-constants';
const 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',
},
];
function Item({ title }) {
return (
<View style={styles.item}>
<Image style={{height:50,width:50}} source={{uri:'https://source.unsplash.com/random'}} />
<Text style={styles.title}>{title}</Text>
<Text style={styles.title2}>{title}</Text>
<Image style={{height:15,width:15,marginLeft:20}} source={{uri:'https://source.unsplash.com/random'}} />
</View>
);
}
export default function App() {
return (
<SafeAreaView style={styles.container}>
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.id}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
item: {
backgroundColor: '#f9c2ff',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
flexDirection:'row'
},
title: {
fontSize: 10,
},
title2:{
fontSize:12 , marginLeft:10
}
});
I am trying to expand or reduce view within a Flatlist when clicking on them based on their current state. I am using data within mobx and connecting it to a react component. Even though console.log confirms that the state is changing after clicking on the view, it does not expand when clicked upon. It was working when the data was a react state, but I had to move it to mobx due to diverse interactions within the software. I think the action syntax and logic has to be changed or it might something else. Please can anyone help?
mobx code below:
import {observable, action} from 'mobx';
import {LayoutAnimation} from 'react-native'
class StateStorage {
#observable materials = [
{
name: 'RAG',
price: '$',
image: require("./Icons/RAG.jpg"),
expanded: false
},
{
name: 'GRAPE',
price: '$',
image: require("./Icons/GRAPE.jpg"),
expanded: false
},
{
name: 'FLAG',
price: '$',
image: require("./Icons/FLAG.jpg"),
expanded: false
},
#action changeLayout(index) {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.materials[index].expanded= !this.materials[index].expanded
console.log(this.materials[index].expanded)
}
#action chooseMaterial(index){
this.selectedMaterial = this.materials[index].name
console.log(this.selectedMaterial)
}
export default new StateStorage();
React-native code below:
import React, { Component } from 'react';
import { View, Text, FlatList, Image, ImageBackground, PixelRatio, Platform, UIManager, TouchableOpacity, LayoutAnimation }
from 'react-native';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen'
import DropDownItem from 'react-native-drop-down-item';
import StateStorage from './StateStorage';
import {observer} from 'mobx-react';
#observer
class App extends Component {
constructor (props) {
super(props)
this.state ={
}
if (Platform.OS === 'android') {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
}
render() {
return (
<View
style={{
backgroundColor:'#262A2C',
flex:1
}}>
<FlatList
style={{marginTop:80,}}
data={StateStorage.materials}
renderItem={({ item, index }) => (
<View style={{}}>
<TouchableOpacity
onPress={() =>{
StateStorage.chooseMaterial(index)
}}>
<ImageBackground
source={item.image}
//pay FlatIcon or design personal one
style={{
resizeMode: 'contain',
position:'relative',
width: wp('100%'),
left: wp('0%'),
borderBottomWidth: 1,
borderBottomColor: 'grey',
padding: hp('6%'),
}}
>
</ImageBackground>
</TouchableOpacity
<TouchableOpacity activeOpacity={0.8}
onPress={() => {
StateStorage.changeLayout(index)
}}
style={{ padding: 10,
backgroundColor:'black',
left:wp('-10.9%'),
top:hp('0%'),
width: wp('120%'),
height:hp('5%')}}>
<Image
style={{
width:wp('9%'),
height:hp('4.5%'),
tintColor:'white',
left:250,
top:-10
//tintColor:'#81F018'
}}
source={StateStorage.materials[index].expanded ? require('./Icons/arrowDown.png') : require('./Icons/arrowUp.png') }/>
</TouchableOpacity>
<View style={{height: StateStorage.materials[index].expanded ? null : 0,
overflow: 'hidden',
backgroundColor:'black' }}>
<Text
style={{
fontSize: 17,
left:150,
top:-10,
color: 'turquoise',
padding: 10}}>
Specs
</Text>
</View>
</View>
)}
}
export default App
Before the state was moved to mobx, I was using this method to make it work:
changeLayout = ({index}) => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.setState(({ materials }) => ({
materials: materials.map((s, idx) =>
idx === index ? {...s, expanded: !StateStorage.materials[index].expanded} : {...s, expanded: false})
}));
console.log(StateStorage.materials[index].expanded)
}
You are searching for accordion :
Please refer to the native base accordion component. This is what you need.
https://docs.nativebase.io/Components.html#accordion-def-headref
react-navigation error img.
I am using react-navigation in my app. Tried to do some test with -native-base side bar. and when you click on the any nav item inside side bar it shows this error. How can it be solved for this side bar? Here is the code:
import React, { Component } from "react";
import { View, ScrollView,Button,Text,StyleSheet,Dimensions } from "react-native";
import {Container,Content,List,Icon,Badge,ListItem,Left,Right,Body} from 'native-base'
import Exponent from 'expo'
import { DrawerItems } from 'react-navigation';
const screenHeight = Dimensions.get('window').height;
const datas = [
{
name: "Home",
route: "HomeScreen",
icon: "phone-portrait",
bg: "#C5F442",
},
{
name: "Business",
route: "Business",
icon: "easel",
bg: "#C5F442",
},
{
name: "Education",
route: "Education",
icon: "phone-portrait",
bg: "#477EEA",
types: "8",
},
{
name: "Life Style",
route: "Lifestyle",
icon: "phone-portrait",
bg: "#DA4437",
types: "4",
},
{
name: "Tech",
route: "Tech",
icon: "notifications",
bg: "#4DCAE0",
},
{
name: "Contact",
route: "Contact",
icon: "radio-button-off",
bg: "#1EBC7C",
types: "9",
},]
const DrawerContent = (props) => {
let showItems = props.items.filter(itemObj => {
return itemObj.routeName !== 'Welcome'
&& itemObj.routeName !== 'Signin'
&& itemObj.routeName !== 'Signup'
})
return (
<View style={style.container}>
<Content>
<List
dataArray={datas}
renderRow={data =>
<ListItem button noBorder onPress={() => this.props.navigation.navigate(data.route)}>
<Left>
<Icon active name={data.icon} style={{ color: "#777", fontSize: 26, width: 30 }} />
<Text>
{data.name}
</Text>
</Left>
{data.types &&
<Right style={{ flex: 1 }}>
<Badge
style={{
borderRadius: 3,
height: 25,
width: 72,
backgroundColor: data.bg,
}}
>
<Text>{`${data.types} Types`}</Text>
</Badge>
</Right>}
</ListItem>}
/>
</Content>
</View>
)
}
const style = StyleSheet.create({
container: {
height: screenHeight,
backgroundColor: '#494949',
flex:1,
paddingTop: Exponent.Constants.statusBarHeight,
},
});
export default DrawerContent;
Is there any solution for this error and for this code?
I also faced the similar problem ,use it like this
In your class where you are using sidebar class declare a function for navigation
goto(route) {
console.log("navigation" + route);
const { navigate } = this.props.navigation;
navigate(route);
}
Then bind this function to the side bar
content={<DrawerContent route={this.goto.bind(this)} />} >
Then in sidebar file handle on press like this
onPress={() => this.props.route('Page Name')}