Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native touchable highlight and touchable native feedback for making my menu items

Tags:

react-native

I am trying to implement a basic drawer, but I am confused by the documentation.

There is a TouchableHighlight, and there is a TouchableNativeFeedback.

TouchableNativeFeedback is Android only. So, how can I best handle my MenuItem component so it takes care of both Android and IOS?

Here's what I've got so far, and I'm not sure how to handle the different platform specific touchable stuff here:

import React, { Component, PropTypes } from 'react';
import { TouchableHighlight, TouchableNativeFeedback, Platform, View, Text } from 'react-native';

export class MenuItem extends Component {

  handleAndroid() {

  }

  handleIOS() {

  }

  renderMenuItem() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>
          {this.props.text}
        </Text>
      </View>
    );
  }
  render() {
    return (

    );
  }
}

On Android, do I have to use TouchableNativeFeedback only?

like image 316
user1354934 Avatar asked Oct 14 '16 20:10

user1354934


People also ask

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 are touchable components in React Native and which one to use when?

Touchable components provide the capability to capture the tapping functionality. The touchables component can be implemented as an alternative of basic button, if they are not look right for your app. Using these components, you build your own button. Tapping on these components, you can display the feedback.

What is Touchablenativefeedback?

A wrapper for making views respond properly to touches (Android only). On Android this component uses native state drawable to display touch feedback.

What component do we use to respond properly for touches in React Native?

Touchables​ If the basic button doesn't look right for your app, you can build your own button using any of the "Touchable" components provided by React Native. The "Touchable" components provide the capability to capture tapping gestures, and can display feedback when a gesture is recognized.


1 Answers

Like you said, TouchableNativeFeedback is Android only therefore it won't work for iOS. If you want a solution that works for both, use TouchableHighlight and style it accordingly. For example, its underlayColor prop allows you to set "the color of the underlay that will show through when the touch is active."

EDIT:

If you want to use TouchableNativeFeedback for Android and TouchableHighlight for iOS, you should do something like the following:

import { Platform } from 'react-native'
...

render() {
    if (Platform.OS === 'android') {
       return (
          <TouchableNativeFeedback> 
               ... 
          </TouchableNativeFeedback>    
       )
    } else {
       return (
          <TouchableHighlight> 
               ... 
          </TouchableHighlight>    
       )
    }
}

EDIT 2:

Alternatively, you can create a custom component for each platform and use file extensions. For example:

MyButton.ios.js
MyButton.android.js

Put these two in the same folder and then use MyButton as a regular component:

import MyButton from '../path/to/component/MyButton'
...

render() {
   <MyButton/>
}

This is quite neat when you want to use this component in multiple places because you don't fill your code with if-else blocks.

Here you can read more about Platform Specific Code.

like image 75
Georgios Avatar answered Sep 30 '22 20:09

Georgios