In this article we will cover the most important aspects of TouchableWithoutFeedback
component.
1. It allows to raise click event for enclosed component. Like the onPress
will be called when component is touched –
<TouchableWithoutFeedback onPress={() => alert('Component1 Pressed!')}> <Component1 /> </TouchableWithoutFeedback>
2. Doesn’t show any visual change in appearance when the component is clicked.
3. React Native do not recommend using this. Better to use pressable.
4. It accepts a single component as child. If you want to render multiple components then wrap them in a View
. Like this –
<TouchableWithoutFeedback onPress={() => alert('Some Child Pressed!')}> <View> <Component1 /> <Component2 /> </View> </TouchableWithoutFeedback>
5. You should pass the props to the underlying component and React Native components. Like this –
const Component1 = (props) => { return <Text {...props}>You can press this text</Text> } <TouchableWithoutFeedback onPress={() => alert('Component1 Pressed!')}> <Component1 /> </TouchableWithoutFeedback>
6. You can disable the touch sound using touchSoundDisabled=true
prop in Android apps.
7. If you want to capture long press with some delay, then use delayLongPress
prop and provide milliseconds value.
8. To disable all the interactions with TouchableWithoutFeedback
, use disabled=true
prop.
Code Example
import React, { useState } from "react"; import { StyleSheet, TouchableWithoutFeedback, Text, View } from "react-native"; const TouchableWithoutFeedbackExample = () => { const [countOnPress, setCountOnPress] = useState(0); const [countOnLongPress, setCountOnLongPress] = useState(0); const [countOnLongPressDelay, setCountOnLongPressDelay] = useState(0); const [countOnPressDisabled, setCountOnPressDisabled] = useState(0); const onPress = () => { setCountOnPress(countOnPress + 1); }; const onLongPress = () => { setCountOnLongPress(countOnLongPress + 1); } const onLongPressDelay = () => { setCountOnLongPressDelay(countOnLongPressDelay + 1); } const onPressDisabled = () => { setCountOnPressDisabled(countOnPressDisabled + 1); }; return ( <View style={styles.container}> <View style={styles.countContainer}> <Text style={styles.countText}>Count onPress: {countOnPress}</Text> <Text style={styles.countText}>Count onLongPress: {countOnLongPress}</Text> </View> <TouchableWithoutFeedback onPress={onPress} onLongPress={onLongPress} > <View style={styles.button}> <Text>Touch Here</Text> </View> </TouchableWithoutFeedback> <View style={styles.countContainer}> <Text style={styles.countText}>Count onLongPress After 3 sec: {countOnLongPressDelay}</Text> </View> <TouchableWithoutFeedback onLongPress={onLongPressDelay} delayLongPress={3000} > <View style={styles.button}> <Text>Long press will work in 3 sec</Text> </View> </TouchableWithoutFeedback> <View style={styles.countContainer}> <Text>Press on below button will not work. disabled=true</Text> <Text style={styles.countText}>Count onPress: {countOnPressDisabled}</Text> </View> <TouchableWithoutFeedback onPress={onPressDisabled} disabled={true} > <View style={styles.button}> <Text>Press Disabled</Text> </View> </TouchableWithoutFeedback> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", paddingHorizontal: 10 }, button: { alignItems: "center", backgroundColor: "#DDDDDD", padding: 10 }, countContainer: { alignItems: "center", padding: 10 }, countText: { color: "#FF00FF" } }); export default TouchableWithoutFeedbackExample;
Output –
In the above image we can see three buttons. 1st is with default values of onPress
and onLongPress
. 2nd button has a custom delay of 3 seconds in long press. The third button is disabled.
Live Demo
Conclusion
TouchableWithoutFeedback
should be used when you want to add click listener to a component but do not want to provide any visual change on click. Use this component only when necessary otherwise pressable should be used.
React Native Series
Alert
- Basic Alert
- Dismiss on Clicking Outside
- Input Fields in Alert Dialog – Prompt()
- Dark-Light Theme of Dialog
ActivityIndicator
- Basic Circular Loader
- Change size of Circular Loader
- Show/hide Circular Loader
- Change color of Circular Loader
Button
FlatList
- Simple List
- Single Item Selection from List
- Multiple Item Selection from List
- Adding separator between list items
- Multiple columns List
- Showing Message in Empty List
- Add Footer to the List
- Add Header to the List
- Horizontal List
- Inverted List
- Pull to Refresh in List
- Infinite Loading List
SectionList
ScrollView
- ScrollView
- Stick Single Item at Header
- Stick Multiple Items at Header
- Stick Item at Footer
- Hide Sticky Element on Scroll
Image
- Display Image from remote url
- Display local storage image
- Display Base64 Image
- Display Gif & Webp Images
- Adding Blur to Image
- Displaying loader for Image
- Resizemode for Images
- Setting Default Placeholder Image
- Background Image
Modal
RefreshControl
- RefreshControl
- Change Refresh Loader Color
- Change Refresh Loader Size
- Change Refresh Loader Background Color
- Title under Refresh Loader
- Change color of title under refresh loader
StatusBar
- Get StatusBar Size
- Change StatusBar Background Color
- Display StatusBar icons & text in While Color
- Display StatusBar icons & text in Dark Color
- Hiding StatusBar
- Translucent StatusBar
Switch
Text
- Adding Text
- Bold Text
- Italic Text
- Underline Text
- Selecting Text for copy-paste
- Changing Highlight Color of Text Selection
- Fit text in View box
- Clickable anchors in text
- Truncate Lengthy Text
TextInput
- Simple Input Field
- Auto Capitalize Text in Input Field
- Multiline Input Field
- Hide Cursor in Input Field
- Clear input Field using X
- Clear input Field when focused
- Change Cursor Color in Input Field
- Disable input field
- Icon at the left of Input Field
- Dark-Light Keyboard
- Avoid Overlapping of Keyboard
- Limiting Characters in Input Field
- Numeric Keyboard
- Email Id Keyboard
- Phone number Keyboard
- Url Keyboard
- Placeholder in input field
- Placeholder Color in Input Field
- Password Input Field
- Programmatically select text in Input Field
- Change Text Selection Color in Input Field
- Select Whole text in Input Field on Focus
- Write text from center in input field
- Changing underline color of input field
TouchableWithoutFeedback
TouchableHighlight
TouchableOpacity
Pressable
Appearance
AppState
ToastAndroid
Dimensions
Keyboard