Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native - TouchableHighlight: Remove highlighting after onPress?

Tags:

I am developing a simple react-native app and am encountering an issue on TouchableHighlight:

When pressing the TouchableHighlight, a new screen is displayed (using StackNavigator from react-navigation). After pressing the back-button and returning to the original screen, the TouchableHighlight still has a black background-color - meaning, that it is still highlighted.

My questions are:

  • Is there a way to manually deactivate the highlighting of a TouchableHighlight-component? That way I could disable the highlighting after onPress has run.
  • What could be possible reasons to why the TouchableHighlight stays highlighted? I am using it on other parts of my app without navigation, and I could imagine that it has to do with that.

The TouchableHighlight exists within a FlatList. The renderItems-method looks like the following:

let handlePress = () => {
    this.props.navigation.navigate('DetailsScreen');
};
return <TouchableHighlight
    onPress={handlePress}>
    <Text>Some Text</Text>
</TouchableHighlight>;

If you need/want any further information, please let me know. I've tested the code on android, using the Genymotion-emulator with Marshmallow.

Versions are:

  • node -v: 8.9.4
  • npm -v: 5.6.0
  • react-native-cli: 2.0.1
  • react-native: 0.54.2
  • react-navigation: 1.5.2
  • Build environment: Windows 10 64-bit

At this point, I'm quite certain that the error is somewhere in my code, as TouchableHighlight works correctly on other parts of my app, and it propably has to do with the navigation-call, but I was unable to pinpoint, why exactly. I've made sure that there are no exceptions or anything like that in my app, and that the onPress-method therefore finishes successfully.

like image 204
Martin Bories Avatar asked Mar 15 '18 23:03

Martin Bories


People also ask

How do I turn off TouchableOpacity animation in react native?

We will introduce how to disable the button in react. js using a disabled prop to our button element. To simply disable the button, we can use the disabled prop in our button element and set its value to true . If we want to disable our button after clicking on it, We can disable it by using react's state.

What is difference between TouchableOpacity and TouchableHighlight?

TouchableOpacity increases the lighteness of a button when tocuhed while TouchableHighlight increases the darkness of a button when touched.

What does TouchableHighlight do and when do you use it?

TouchableHighlight is a component that is used to provide a wrapper to Views in order to make them respond correctly to touch-based input. On press down the TouchableHighlight component has its opacity decreased which allows the underlying View or other component's style to get highlighted.

How do I change the touchable opacity color in react native?

You can write your code like: import React, {Component} from 'react'; import {Platform, StyleSheet, Text, View, Button,TouchableOpacity} from 'react-native'; export default class App extends Component { state={ backgroundColor: 'black', backgroundColor2: 'black', pressed: false, }; changeColor(){ if(!


1 Answers

You can replace Touchable Highlight with Touchable opacity and simply set activeOpactity prop with value 1. It will not highlight the press.

<TouchableOpacity activeOpacity={1}>....</TouchableOpacity>
like image 158
Yogendra Solanki Avatar answered Sep 22 '22 13:09

Yogendra Solanki