Parameter Routes in react-native - react-native

Formy post screen I want to take the title and body of each one
import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, Image, Text, View, Dimensions, TouchableWithoutFeedback } from 'react-native';
import { withNavigation } from 'react-navigation';
class Card extends Component {
render() {
return (
<TouchableWithoutFeedback onPress={() => this.props.navigation.navigate('Post')}>
<View style={{
flexDirection: 'row',
backgroundColor: 'white',
borderBottomWidth: 1,
borderRightWidth: 1,
borderLeftWidth: 1,
borderColor: 'rgba(200,200,200,0.3)',
borderRadius: 6,
width: '86%',
maxHeight: 120,
minHeight: 120,
alignSelf: 'center',
marginBottom: 5
}}>
<Image style={{
width: 100,
maxHeight: 100,
minHeight: 100,
borderRadius: 20,
}}
source={{ uri: 'https://im.ziffdavisinternational.com/ign_br/screenshot/default/screenshot-2_5s54.png',
}}
/>
<View style={{
width: '66%',
padding: 5}}
>
<Text style={{
fontSize: 14,
fontWeight: 'bold',
fontFamily: 'verdana',
}}
numberOfLines={2}
>
{this.props.item.title}
</Text>
<Text style={{
fontSize: 12,
marginTop: 5
}} numberOfLines={3}
>
{this.props.item.body}
</Text>
</View>
</View>
</TouchableWithoutFeedback>
);
}
}
export default withNavigation(Card);

The question is very unclear about what you want to accomplish with a very little piece of information.
If the question is about passing parameters from a screen to another (in this case, title and body), using react navigation, then you just have to change your navigate function in the TouchableWithoutFeedback to:
this.props.navigation.navigate('Post',{ "title":this.props.item.title, "body": this.props.item.body })
Then, when you want to use them in the destination screen (in this case, Post), you can access them using:
this.props.navigation.state.params.title //or this.props.navigation.state.params.body
or do
var title=this.props.navigation.getParam("title")

Related

How to fix issue with KeyboardAwareScrollView?

Screenshot
Screenshot2
import React from 'react';
import { View, SafeAreaView, StyleSheet, TextInput, Text, TouchableOpacity, Platform } from 'react-native';
import MapView, { UrlTile } from 'react-native-maps';
import tw from 'tailwind-react-native-classnames';
import { Card } from 'react-native-shadow-cards';
import { useNavigation } from '#react-navigation/native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
const MapScreen = () => {
const navigation = useNavigation();
let location = {
latitude: 5.354846154402075,
longitude: 100.30152659738222,
latitudeDelta: 0.001,
longitudeDelta: 0.002
}
return (
<SafeAreaView style={tw`bg-white h-full`}>
<View style={tw`h-2/3`}>
<MapView
style={styles.map}
mapType='standard'
region={location}>
<UrlTile
urlTemplate='https://api.maptiler.com/maps/openstreetmap/256/{z}/{x}/{y}.jpg?key=2fFGEOiBVDCSMRNfaU70'
shouldReplaceMapContent={true}>
</UrlTile>
</MapView>
</View>
<KeyboardAwareScrollView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
bounces={false}
enableOnAndroid={true}
scrollEnabled={true}
enableAutomaticScroll={true}
contentContainerStyle={{ flex: 1 }}>
<View style={styles.cardContainer}>
<Card style={styles.cardView}>
<Text style={{ fontSize: 11, paddingTop: 10, paddingLeft: 10 }}>From</Text>
<View style={styles.inputBox}>
<TextInput
style={{ paddingVertical: 0, paddingLeft: 10 }}
placeholder='Current Location'
keyboardType='current-location'>
</TextInput>
</View>
<Text style={{ fontSize: 11, paddingTop: 10, paddingLeft: 10 }}>To</Text>
<View style={styles.inputBox}>
<TextInput
style={{ paddingVertical: 0, paddingLeft: 10 }}
placeholder='Destination'
keyboardType='destination'>
</TextInput>
</View>
<View>
<TouchableOpacity
onPress={() => navigation.navigate('ShowResultScreen')}
style={styles.button}>
<Text
style={{
textAlign: 'center',
fontWeight: '700',
fontSize: 17,
color: '#fff'
}}>
Search
</Text>
</TouchableOpacity>
</View>
</Card>
</View>
</KeyboardAwareScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
map: {
width: '100%',
height: '100%',
},
cardContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 25,
shadowOpacity: 0.8
},
cardView: {
width: '97%',
height: '95%',
paddingLeft: 10,
paddingTop: 3
},
button: {
backgroundColor: '#5AA9E6',
padding: 10,
borderRadius: 10,
marginTop: 15,
marginRight: 10
},
inputBox: {
borderRadius: 10,
width: '97%',
height: '15%',
backgroundColor: '#F5F3F4',
marginTop: 10
}
});
export default MapScreen;
I want to make the input section below is scrollable and not covered by the keyboard when the keyboard is pop up so that the Search button can be pressed after typing the required input.
Update: I have used KeyboardAwareScrollView instead of using KeyboardAvoidingView and ScrollView. However, the result is like the Screenshot2.
The easiest way is to use the package react-native-keyboard-aware-scroll-view.

Problem with placing a pressable text in react native

I am working on a react native application and I was dealing with Modal and Pressable components, in fact the problem I got is not an error or a bug but I was trying to place a pressable text at the very right down side of the screen but I am struggling with styling a little bit so if anyone can give me a hand in this I would be so grateful.
this the code I wrote for the pop up form component:
import React from 'react';
import {View, Text, Modal, StyleSheet, Pressable, Alert} from 'react-native';
const PopUpModal = ({visible, onPress})=>{
return(
<View style={styles.container}>
<Modal
animationType="slide"
transparent={true}
visible={visible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.container}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>
</View>
</Modal>
</View>
)
}
const styles = StyleSheet.create({
container:{
width: "100%",
height : "100%"
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 5,
padding: 35,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
button: {
display: "flex",
borderRadius: 2,
padding: 10,
flexDirection:"row-reverse",
width: 65,
backgroundColor: "#051316",
},
buttonClose: {
},
textStyle: {
color: "white",
fontWeight: "bold",
textAlign: "center",
alignItems: "flex-end"
},
modalText: {
marginBottom: 15,
flexDirection: "row-reverse"
}
})
export default PopUpModal
and this is what it looks like as an output of the above code example
output pic
Put your Pressable inside a View with the style property flexDirection:"row-reverse"
<View style={{flexDirection:"row-reverse"}}>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>

How to change the TouchableOpacity image on press

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image } from 'react-native';
import check from './assets/icons/covid.png'
import pill from './assets/icons/pill.png'
export default function App() {
return (
<View style={styles.container}>
<View style={styles.containerone}>
<TouchableOpacity style={{borderRadius: 100, height: 150, width: 150, backgroundColor: '#fff', marginTop: 80}}>
<Image source={pill} style={styles.pillButton}/>
<Image source={check} style={styles.checkButton}/>
</TouchableOpacity>
</View>
<View style={styles.containertwo}>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text b</Text>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text a</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#982fc2'
},
containerone: {
flex: 1,
alignItems: 'center'
},
containertwo: {
flex: 1,
backgroundColor: '#fff',
borderTopRightRadius: 60,
borderTopLeftRadius: 60,
padding: 40
},
button: {
backgroundColor: '#fff',
borderRadius: 100,
height: 150,
width: 150,
marginTop: 100,
alignItems:'center',
justifyContent: 'center'
},
checkButton: {
height: 130,
width: 130,
display:'none'
},
pillButton: {
height: 130,
width: 130,
display: 'flex',
marginTop: 10
}
});
I have a simple question, how can I change the style of two buttons in press the first:
I created one style for each button, the first button is visible, and the second is hidden, so, I want to, when I press the first button, I change it style to display: 'none', and in the second, to display:'flex', can you guys help me?
Ciao, you colud use state to alternate Image visibility avoding to use css. Something like:
export default function App() {
const[alternateImage, setAlternateImage] = useState(true);
const changeImage = () => {
setAlternateImage(alternateImage => !alternateImage);
}
return (
<View style={styles.container}>
<View style={styles.containerone}>
<TouchableOpacity style={{borderRadius: 100, height: 150, width: 150, backgroundColor: '#fff', marginTop: 80}} onPress={changeImage}>
{alternateImage && <Image source={pill} style={styles.pillButton}/>}
{!alternateImage && <Image source={check} style={styles.checkButton}/>}
</TouchableOpacity>
</View>
<View style={styles.containertwo}>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text b</Text>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text a</Text>
</View>
</View>
);
}
and you could remove display: 'none' from Image css.

React Native app with same code but different result for vertical margins in iOS and Android

I tested a simple render() on android (Galaxy S7) and iOS (iPhone S8+), and I get results that I don't understand.
The S7's height in dp's (Density-independent Pixels) is 640, while the iPhone 8 Plus height is 736 dp's, so I expected the distance between 'email' and 'password' to be somewhat smaller on the iPhone 8+, but not that tiny...
The 2nd issue is negative margins, that seem to behave differently on the two platforms. Is that what one should expect?
(And, yes, I know that I can set different margins on the two platforms, but I want to understand why the results are so different from my expectations...)
This is my code:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: 25 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='email'
/>
<View style={{ marginTop: -10,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: 5 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='password'
/>
<View style={{ marginTop: -10,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}
And this is how this code is displayed on an android Galaxy S7 emulator (left) and iPhone 8+ emulator.
I know this isn't your question, and i saw your profile and you're a react god yourself, and i respect that a lot haha
But my question is, why is the code like this, instead of being something like:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ alignItems: 'center' }}>
<TextInput
style={{color: 'black', width: 260, borderBottomWidth : 4, marginTop: 25}}
value='email'
/>
<TextInput
style={{color: 'black', width: 260, borderBottomWidth : 4, marginTop: 5}}
value='password'
/>
</View>
);
}
}
Do this code works the same way?
[EDIT]
Also, maybe it's because some problems with the IOS top and bottom bar (On newer iphones, wich is not the case). So maybe the "Top" of the Android isn't the same "Top" as the IOS because Android apps doesn't overlaps the top bar like IOS do.
So you can make a conditional check to change the IOS MarginTop value, like this:
import React, { Component } from 'react';
import { View, TextInput, Platorm } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: Platform.OS == 'android' ? 25 : 35 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='email'
/>
<View style={{ marginTop: Platform.OS == 'android' ? -10 : -5,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: Platform.OS == 'android' ? 5 : 15}}>
<TextInput
style={{ color: 'black', width: 260 }}
value='password'
/>
<View style={{ marginTop: Platform.OS == 'android' ? -10 : -5,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}
I think this should work as fine.
The other answer and comment were good, but they didn't answer my question... :)
I asked why there is such a difference between the two platforms and not how it can be modified with platform specific values.
I found the answer here:
react-native TextInput displays wrong when changing height on Android
quote: Android adds some default padding on top and bottom, you can reset them by adding paddingVertical: 0 to your element' style.
When specifying paddingVertical: 0, we get the expected results:
This is the updated code:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: 25 }}>
<TextInput
style={{ color: 'black', width: 260, paddingVertical: 0 }}
value='email'
/>
<View style={{ marginTop: 0,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: 15 }}>
<TextInput
style={{ color: 'black', width: 260, paddingVertical: 0 }}
value='password'
/>
<View style={{ marginTop: 0,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}

How to design full Center Cut FAB in react native navigation

I am need to create a custom bottom navigation with floating action button as a full center cut in react native. I will attach the image. I need exactly this and there is not tutorial to follow as at now.I need help. You can drops links I can follow.If you have done something like this,you can share the code
I did like this in my one project. create "BottomNavigator" component and import it at any class that I wish:
import React, { Component } from 'react';
import { View } from 'react-native';
import { Icon, Button } from 'react-native-elements'
class BottomNavigator extends Component {
render() {
return (
<View style={{
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff'
}}>
<View style={{ position: 'absolute', padding: 5, alignSelf: 'center', backgroundColor: '#fff', width: 70, height: 70, borderRadius: 35, bottom: 25, zIndex: 5 }}>
<Button
icon={{
name: 'plus',
type: 'feather',
color: '#fff',
style: { marginRight: 0 }
}}
onPress={() => this.doSomething()}
buttonStyle={{ backgroundColor: '#000', width: 60, height: 60, borderRadius: 30 }}
containerViewStyle={{ alignSelf: 'center' }}
/>
</View>
<View style={{ position: 'absolute', backgroundColor: '#3F51B5', bottom: 0, zIndex: 1, width: '100%', height: 60, flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 15, paddingVertical: 10 }}>
<Icon
name='list'
type='feather'
color='#fff'
onPress={() => this.doSomething()} // Ex : openDrawer() in react-navigation
/>
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
<Icon
name='heart'
type='feather'
color='#fff'
containerStyle={{ marginHorizontal: 10 }}
/>
<Icon
name='search'
type='feather'
color='#fff'
/>
</View>
</View>
</View>
);
}
}
export default BottomNavigator;
And in any class import and use <BottomNavigator />.
I am using react-native-elements and vector-icons. It's not necessary just recommended.
Used inline style for your easy edit. I hope this helps you.