question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Touchable] easy way to disable a Touchable* highlight?

See original GitHub issue

Hi,

the current pattern I’ve been doing for making Buttons that can be “disabled” is to do the following:

return !disabled ? <TouchableOpacity onPress={onPress}>{btn}</TouchableOpacity> : btn;

I need to do that because a disabled button should not have any “highlight” effect.

The problem with such approach is it changes the component tree which means btn will be re-instanciated every time disabled changes. One of the consequence is that if btn wants to do animation, the animation will be suddenly stopped.


Here is my proposal to fix that,

what if I could do that?

return <TouchableOpacity onPress={!disabled ? onPress : null}>{btn}</TouchableOpacity>;

I’m proposing that if onPress is falsy, there should be no highlight effect at all on a Touchable* components. This would also make the code more maintainable because you don’t have to branch your logic.

What do you think?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Kureevcommented, Feb 15, 2016

What if we can patch Touchable mixin?

touchableHandleStartShouldSetResponder: function() {
  return !this.props.disabled;
},

should do the trick. I can send over a PR along with adding a new propType

disabled: React.PropTypes.bool

to TouchableWithoutFeedback base class. What do you think?

0reactions
TheoGitcommented, Sep 7, 2016

Awesome!

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-native - Remove highlighting after onPress?
You can replace Touchable Highlight with Touchable opacity and simply set activeOpactity prop with value 1. It will not highlight the press.
Read more >
TouchableHighlight - React Native
A wrapper for making views respond properly to touches. On press down, the opacity of the wrapped view is decreased, which allows the ......
Read more >
Disable TouchableOpacity - Swair AQ - Medium
First import TouchableOpacity from react-native and don't do a mistake I did back in my time and let my app auto import it...
Read more >
Prevent the touch keyboard from appearing - Microsoft Support
Note: Some third-party software may disable the service again after you have reenabled it. If this is happening, the best way to track...
Read more >
TouchableOpacity vs TouchableHighlight // ReactNative ...
Thanks to all the channel supporters, I can create a new full course for React Professionals looking to quickly level up to React...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found