react native keyboard does not dismiss why? - react-native

I have a keyboardavoidingview with flex 1 with an onPress function that should dismiss the keyboard but nothing happens.
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, useWindowDimensions, KeyboardAvoidingView, Keyboard } from 'react-native';
import { TouchableOpacity, ScrollView, TextInput } from 'react-native-gesture-handler';
import { AntDesign } from '#expo/vector-icons';
const Home = ({ navigation }) => {
const width = useWindowDimensions().width;
const height = useWindowDimensions().height;
return (
<KeyboardAvoidingView onPress={Keyboard.dismiss} style={styles.container}>
<View style={styles.header}>
<View style={styles.headerTop}>
<TouchableOpacity>
<AntDesign name="pluscircleo" size={42} color="#fff" />
</TouchableOpacity>
</View>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Text style={styles.title}>Restaurant</Text>
</View>
<View style={styles.inputContainer}>
<TextInput style={styles.input} placeholder="Find Restuarants" />
</View>
</View>
<StatusBar backgroundColor="#fff" />
</KeyboardAvoidingView>
)
};
const styles = StyleSheet.create({
container: {
flex: 1
},
header: {
backgroundColor: 'red',
},
headerTop: {
paddingTop: 30,
},
title: {
fontSize: 22,
color: '#fff',
},
inputContainer: {
justifyContent: 'center',
alignItems: 'center',
margin: 10
},
input: {
color: '#333',
borderColor: '#fff',
borderRadius: 5,
padding: 6,
backgroundColor: '#fff',
width: 290
}
});
export default Home;
if I delete keyboard.dismiss and add console.log('test') then again nothing happens.
Where is my issue?
thanks for your help!

You should wrap the KeyboardAvoidingView with TouchableWithoutFeedback and remove onPress from the KeyboardAvoidingView and put it in the TouchableWithoutFeedback
import { StyleSheet, Text, View, useWindowDimensions, KeyboardAvoidingView, Keyboard, Platform, Alert, TouchableWithoutFeedback } from 'react-native';
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<KeyboardAvoidingView style={styles.container}>
</KeybaordAvoidingView />
</TouchableWithoutFeedback>
Link here

Related

react native shows nothing if I use touchablewithoutfeedback

I create a form but if I add touchablewithoutfeedback then it shows nothing more. If I use View its show but why?
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, ImageBackground, TextInput, Dimensions, KeyboardAvoidingView } from 'react-native';
import { TouchableOpacity, ScrollView, TouchableWithoutFeedback } from 'react-native-gesture-handler';
import { Ionicons } from '#expo/vector-icons';
const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;
const createRoom = () => {
return (
<TouchableWithoutFeedback style={{flex: 1,alignItems: 'center', justifyContent: 'center'}}>
<KeyboardAvoidingView behavior="position">
<View style={{backgroundColor: 'red', width: width * 0.8, padding: 20, borderRadius: 8, elevation: 2}}>
<View style={{marginTop: 20, marginBottom: 20}}>
<TextInput placeholder="Username" style={styles.input} />
</View>
<View style={styles.locationContainer}>
<TouchableOpacity>
<Text>geolocation</Text>
</TouchableOpacity>
<View>
<Text>Test</Text>
</View>
</View>
<TouchableOpacity style={styles.button}>
<Text>Gruppe erstellen</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
</TouchableWithoutFeedback>
)
};
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff'
},
input: {
padding: 4,
backgroundColor: '#fff',
},
image: {
height: 150,
borderRadius: 12,
},
locationContainer: {
flexDirection: 'row',
justifyContent: 'space-between'
},
button: {
color: '#fff',
justifyContent: 'center',
alignItems: 'center',
padding: 10
}
});
export default createRoom;
i am very thankful for your help. I dont know the issue.......................................................................................................................................
You can change the TouchableWithoutFeedback import from react-native-gesture-handler to react-native.
import {StyleSheet, Text, View, ImageBackground, TextInput, Dimensions, KeyboardAvoidingView, TouchableOpacity, ScrollView, TouchableWithoutFeedback} from 'react-native';
Or you can go for a new component Pressable

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 to design React-Native Button

I am new to react-native and I want to design such a button, I tried and searched but could not find an answer
any help would be appreciated
Look at this aproach, with radiusBorder you can achieve something close to you're looking for:
import React from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.wrapperContainer}>
<View style={styles.Container}>
<Text>Foo</Text>
</View>
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
height: 50,
width: 100,
alignItems: 'center',
background: '#1f90f0',
borderRadius: 5,
justifyContent: 'center',
borderTopLeftRadius: 50,
borderLeftWidth: 2,
borderLeftColor: 'lightgray'
},
wrapperContainer: {
background: 'lightgray',
width: 100,
}
});
You can create a custom button in react-native by using TouchableOpacity and if you want to perform any function when the user press that button you can use onPress prop.
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
const FunctionName = () => {
return(
<View>
<TouchableOpacity style={styles.buttonStyling}>
<Text>Button Name</Text>
</TouchableOpacity>
</View>
);
}
export defaultFunctionName;
Use TouchableOpacity instead. That way you can customize the button however you want it to be.
https://reactnative.dev/docs/touchableopacity
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
export default function NAME() {
return(
<View>
<TouchableOpacity style={{padding: 10, borderWidth: 1, borderColor: '#000'}}>
<Text>Text goes here</Text>
</TouchableOpacity>
</View>
);
}
Edit: This is the closest I got for what you are looking for.
import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.Container}>
<View style={styles.ButtonShape} />
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
backgroundColor: 'transparent',
},
ButtonShape: {
borderLeftWidth: 20,
width: 120,
borderLeftColor: 'transparent',
borderRightColor: 'transparent',
borderTopColor: 'blue',
borderTopWidth: 50,
},
});

React Native Import

Can we use single keyword instead of '' import { Text, TextInput, Button, StyleSheet, View } from 'react-native'; " in react native? Is there any options available Single keyword for "Text, TextInput, Button, StyleSheet, View"?
import { Button, StyleSheet, View } from 'react-native';
export default class ButtonBasics extends Component {
_onPressButton() {
alert('You tapped the button!')
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button
onPress={this._onPressButton}
title="Press Me"
color="#841584"
/>
</View>
<View style={styles.alternativeLayoutButtonContainer}>
<Button
onPress={this._onPressButton}
title="This looks great!"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
buttonContainer: {
margin: 20
},
alternativeLayoutButtonContainer: {
margin: 20,
flexDirection: 'row',
justifyContent: 'space-between'
}
});
You could import ReactNative from 'react-native' and use them as <ReactNative.View/> and so on, but I wouldn't recommend that. It's not how 99% of RN devs would expect it to read.

Element type is invalid: expected a string when using TouchableHighlight

I am having problems when adding the TouchableHighlight component from react-native, and I am given the following error:
Element type is invalid: expected a string
Everything works fine if I delete this
The code is the following:
import React from 'react';
import {View, Text, Image, ScrollView} from 'react-native';
import axios from 'axios';
import {Card, CardItem, Right, Left, TouchableHighlight} from 'native-base';
import { Icon } from 'react-native-elements'
import HeaderMenu from '../Header/Header';
class homeScreen extends React.Component {
state = {
noticias: []
}
componentDidMount() {
axios.get('URL')
.then(res => this.setState({
noticias: res.data
}));
}
render() {
const {navigate} = this.props.navigation;
return (
<View style={{flex: 1}}>
<HeaderMenu title='Noticias'/>
<ScrollView>
<View style={{borderTopColor: 'white', borderTopWidth: 2}}>
{
this.state.noticias.map((noticia, index) => (
<TouchableHighlight onPress={() => navigate('SingleScreen')}>
<CardItem key={index} style={{height: 100, padding: 5, borderBottomColor: '#dee0e2', borderBottomWidth: 1}} >
<View>
<Image style={{width: 120, height: 80, alignSelf: 'stretch'}} source={{uri: noticia.imagen}} />
</View>
<View style={{height: 80, flex: 1, paddingLeft: 10}}>
<Text style={{fontSize: 12, marginBottom: 8}} >{noticia.titulo}</Text>
</View>
<Icon style={{marginTop: 40 ,width: 20, height: '100%', color: '#343537', justifyContent: 'center', paddingLeft: 5}} name="chevron-right" />
</CardItem>
</TouchableHighlight>
))
}
</View>
</ScrollView>
</View>
);
}
}
export default homeScreen;
Already checked other answers but none of them solve my problem.
I found the problem. You are doing this.
import {Card, CardItem, Right, Left, TouchableHighlight} from 'native-base';
You are importing TouchableHighlight from native-base which is wrong. You have to remove it.
You have to import TouchableHighlight from react-native
import {View, Text, Image, ScrollView,TouchableHighlight} from 'react-native';