简体   繁体   English

React Native无法单击TouchableOpacity或TextInput

[英]React Native can't click TouchableOpacity or TextInput

In my React Native project I have a TextInput and a TouchableOpacity component at the top of the screen that for some reason cannot be clicked. 在我的React Native项目中,我在屏幕顶部有一个TextInput和一个TouchableOpacity组件,由于某些原因,它们无法被单击。 It worked at some point while I was working on the project, but for some reason it no longer recognizes clicks or key events. 在我从事该项目时,它有时起作用,但由于某种原因,它不再识别单击或关键事件。 The component is as follows: 组件如下:

 import React, { Component } from "react";
import {
AsyncStorage,
Dimensions,
FlatList,
Platform,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View
} from "react-native";
import styles from "./style/styles";
import NotificationBar from "./components/NotificationBar";

export default class App extends Component {
constructor(props) {
    super(props);
    this.data = [];
    this._initData();
}

_initData() {
    for (let i = 0; i < 500; i++) {
        this.data.push({
            // key: i,
            id: "ABC" + i,
            name: "Random Name",
            value: 50 * i
        });
    }
}

_renderItem = ({ item }) => {
    return (
        <View style={styles.itemContainer}>
            <Text style={styles.textItem}>{item.id}</Text>
        </View>
    );
};

render() {
    return (
        <View>
            <NotificationBar />
            <View style={{ flex: 1, flexDirection: "column" }}>
                <View style={{ flex: 1, flexDirection: "row" }}>
                    <View
                        style={{
                            width: (Dimensions.get("window").width / 3) * 2,
                            height: 50,
                            backgroundColor: "white"
                        }}
                    >
                        <TextInput
                            placeholder="New List Name"
                            style={styles.textInputStyle}
                        />
                    </View>
                    <View
                        style={{
                            width: Dimensions.get("window").width / 3,
                            height: 50,
                            backgroundColor: "#4ce31e"
                        }}
                    >
                        <TouchableOpacity
                            style={styles.button}
                            onPress={this.testSetStorage}
                        >
                            <Text> Create List </Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </View>
            <View style={{ paddingTop: 50 }}>
                <FlatList
                    data={this.data}
                    renderItem={this._renderItem}
                    style={{ alignSelf: "stretch" }}
                    keyExtractor={(item, index) => item.id}
                />
            </View>
        </View>
    );
}
}

The style sheet is: 样式表为:

import { StyleSheet } from 'react-native';

export default StyleSheet.create({
    statusBarBackground: {
        height: 20,
        backgroundColor: 'white',
    },
    textInputStyle: {   
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
        textAlign: 'center',
        borderWidth: 1,
        borderRadius: 6,
    },
    button: {
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
        width: 100,
        paddingLeft: 20,

    },
    container: {
        flex: 1,
        justifyContent: 'center',
        paddingHorizontal: 10
      },
});

My best guess is that it the issue would have something to do with the parent View components that the TouchableOpacity and TextInput components are in, but I'm not sure. 我最好的猜测是,问题可能与TouchableOpacity和TextInput组件所在的父View组件有关,但我不确定。 How can I get those components to recognize clicks and respond to them? 如何获得这些组件来识别点击并做出响应? I'm running on an iPhone 7 emulator. 我在iPhone 7模拟器上运行。

You have not defined testSetStorage function in your component. 您尚未在组件中定义testSetStorage函数。 You need to testSetStorage = () => {console.log("Tapped")} outside render function. 您需要在渲染函数外部进行testSetStorage = () => {console.log("Tapped")}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM