Câu hỏi phỏng vấn React Native
Câu hỏi

What does TouchableHighlight do and when do you use it?

Câu trả lời

TouchableHighlight in React Native

What does TouchableHighlight do?

TouchableHighlight is a React Native component that wraps around a view to make it respond to touch events. When a user presses down on the TouchableHighlight component, the opacity of the wrapped view decreases, allowing the underlay color to show through. This provides visual feedback to the user, indicating that the touch event has been registered. The component must have exactly one child; if multiple children are needed, they should be wrapped in a single View component.

Key Properties:

  • onPress: Function to be called when the touch is released.
  • disabled: If true, disables all interactions.
  • style: Specifies the style of the TouchableHighlight component.
  • activeOpacity: Specifies the opacity value of the wrapped view when the touch is active (default is 0.85).
  • underlayColor: Specifies the color of the underlay that is shown when the touch is active.

Example Usage:

import React from 'react';
import { StyleSheet, Text, View, TouchableHighlight, Alert } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <TouchableHighlight
        onPress={() => { Alert.alert("Touchable Highlight pressed."); }}
        style={styles.touchable}
        activeOpacity={0.5}
        underlayColor="#67c904"
      >
        <Text style={styles.text}>Click Me!</Text>
      </TouchableHighlight>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  touchable: {
    height: 50,
    width: 200,
    borderRadius: 10,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#4287f5'
  },
  text: {
    color: "#fff"
  }
});

In this example, when the "Click Me!" button is pressed, an alert is shown, and the button's background color changes to the specified underlayColor.

When to Use TouchableHighlight:

TouchableHighlight is particularly useful when you want to provide visual feedback for touch even...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

Why do we use curly brace while importing some library?

junior

Will this piece of code work?

middle

In Fast Refresh, what will happen if you edit files imported by modules outside of the React Tree?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào