React Native-Touchable不起作用并且没有错误?

时间:2020-07-13 05:17:28

标签: ios reactjs react-native

我正在尝试在React Native中检测视图上的触摸。我遵循了Dropdown.Header,并在样式视图中合并了Touchable,如下所示:

<TouchableWithoutFeedback onPress={cardPressed}>
       <View style={styles.card}>
          <Text style={styles.card_Head}>What is Something?</Text>
          <Text style={styles.card_Body}>Something</Text>
       </View>
</TouchableWithoutFeedback>

我正在使用功能组件,所以在export default function App() {之后,我有:

function cardPressed()
    {
      console.log('pressed');
    }

没有错误,但我什么也没得到。此实现有什么问题?

2 个答案:

答案 0 :(得分:0)

查看此代码。

import React, { useState } from "react";
import { StyleSheet, TouchableWithoutFeedback, Text, View } from "react-native";

const App = () => {
  const [count, setCount] = useState(0);

  const cardPressed = () => {
     console.log('pressed');
     setCount(count + 1);
  };
 
  return (
    <View style={styles.container}>
      <View style={styles.countContainer}>
        <Text style={styles.countText}>Count: {count}</Text>
      </View>
      
      <TouchableWithoutFeedback onPress={cardPressed}>
        <View style={styles.button}>
          <Text style={styles.card_Head}>What is Something?</Text>
          <Text style={styles.card_Body}>Something</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"
  },
  card_Head: {
    fontWeight: 'bold',
    fontSize: 24
  },
  card_Body: {
    fontStyle: 'italic'
  }
});

export default App;

答案 1 :(得分:0)

检查您的导入。您应该从react-native而不是从react-native-gesture-handler导入TouchableWithoutFeedback