首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnPress不被呼叫

OnPress不被呼叫
EN

Stack Overflow用户
提问于 2020-03-31 17:30:34
回答 3查看 116关注 0票数 0

我正在尝试使用Reacti原住民构建一个应用程序。我做了一个自定义按钮,onPress不起作用,试图寻找答案,但找不到答案,代码看起来就像文档(从那里复制)。

这是自定义按钮:

代码语言:javascript
复制
    import React from 'react';
    import { View, Text, StyleSheet, TouchableHighlight, TouchableOpacity } from 'react-native';
    import COLORS from '../constants/constants';

    class AppButton extends React.Component {
        render() {
            return(
                <View style={styles.container}>
                    <TouchableOpacity>
                        <Text style={styles.title}>
                            {this.props.title}
                        </Text>
                    </TouchableOpacity>
                </View>
              );
        }
    }

    const styles = StyleSheet.create({
      container: {
        backgroundColor: COLORS.appOrange,
        borderRadius: 20,
        elevation: 3,
        flexShrink: 0,
        width: '100%',
        height: 60,
        justifyContent: 'center'
      },
      title: {
          fontSize: 24,
          color: COLORS.appWhite,
          textAlign: 'center',
      },
    });

    export default AppButton;

以下是我尝试使用onPress的方式:

代码语言:javascript
复制
    import React from 'react';
import { View, Text, StyleSheet, TextInput } from 'react-native';
import COLORS from '../constants/constants';
import Card from '../components/card';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import AppTextInput from '../components/appTextInput';
import AppButton from '../components/appButton';

class Login extends React.Component {

    onPress = () => {
      console.log("hello world!@#!@#@!#");
        // this.setState({
        //   count: this.state.count + 1
        // });
    };

    render() {
        return(
            <View style={styles.superContainer}>
              <View style={styles.formContainer}>
                  <AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
                  <AppTextInput placeHolderText="Passwrod"></AppTextInput>
              </View>
              <View style={styles.buttonContainer}>
                  <AppButton
                  title="LOGIN"
                  style={styles.loginButton}
                  onPress={this.onPress}
                  />
              </View>
            </View>

        );
    }
}

我试着看了看控制台,但没有打印出来,尝试了几个不同的例子,我在互联网上看到,但点击就是没有注册。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-03-31 17:36:52

我将OnPress方法作为道具传递给AppButton,并将此方法附加到TouchableOpcaity OnPress事件中。

代码语言:javascript
复制
 import React from 'react';
    import { View, Text, StyleSheet, TouchableHighlight, TouchableOpacity } from 'react-native';
    import COLORS from '../constants/constants';

    class AppButton extends React.Component {
        render() {
            return(
                <View style={styles.container}>
                    <TouchableOpacity onPress={this.props.handleOnPress}>
                        <Text style={styles.title}>
                            {this.props.title}
                        </Text>
                    </TouchableOpacity>
                </View>
              );
        }
    }

    const styles = StyleSheet.create({
      container: {
        backgroundColor: COLORS.appOrange,
        borderRadius: 20,
        elevation: 3,
        flexShrink: 0,
        width: '100%',
        height: 60,
        justifyContent: 'center'
      },
      title: {
          fontSize: 24,
          color: COLORS.appWhite,
          textAlign: 'center',
      },
    });

    export default AppButton;
代码语言:javascript
复制
  import React from 'react';
import { View, Text, StyleSheet, TextInput } from 'react-native';
import COLORS from '../constants/constants';
import Card from '../components/card';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import AppTextInput from '../components/appTextInput';
import AppButton from '../components/appButton';

class Login extends React.Component {

    onPress = () => {
      console.log("hello world!@#!@#@!#");
        // this.setState({
        //   count: this.state.count + 1
        // });
    };

    render() {
        return(
            <View style={styles.superContainer}>
              <View style={styles.formContainer}>
                  <AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
                  <AppTextInput placeHolderText="Passwrod"></AppTextInput>
              </View>
              <View style={styles.buttonContainer}>
                  <AppButton
                  title="LOGIN"
                  style={styles.loginButton}
                  handleOnPress={this.onPress}
                  />
              </View>
            </View>

        );
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-03-31 17:40:16

在您的示例中,您没有将onPress道具传递到自定义按钮组件中的TouchableOpacity中。

尝尝这个

定制按钮

代码语言:javascript
复制
import React from "react";
import {
  View,
  Text,
  StyleSheet,
  TouchableHighlight,
  TouchableOpacity
} from "react-native";
import COLORS from "../constants/constants";

class AppButton extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={this.props.onPress}>
          <Text style={styles.title}>{this.props.title}</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: COLORS.appOrange,
    borderRadius: 20,
    elevation: 3,
    flexShrink: 0,
    width: "100%",
    height: 60,
    justifyContent: "center"
  },
  title: {
    fontSize: 24,
    color: COLORS.appWhite,
    textAlign: "center"
  }
});

export default AppButton;

登录类

代码语言:javascript
复制
import React from "react";
import { View, Text, StyleSheet, TextInput } from "react-native";
import COLORS from "../constants/constants";
import Card from "../components/card";
import { Colors } from "react-native/Libraries/NewAppScreen";
import AppTextInput from "../components/appTextInput";
import AppButton from "../components/appButton";

class Login extends React.Component {
  onPress = () => {
    console.log("hello world!@#!@#@!#");
    // this.setState({
    //   count: this.state.count + 1
    // });
  };

  render() {
    return (
      <View style={styles.superContainer}>
        <View style={styles.formContainer}>
          <AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
          <AppTextInput placeHolderText="Passwrod"></AppTextInput>
        </View>
        <View style={styles.buttonContainer}>
          <AppButton
            title="LOGIN"
            style={styles.loginButton}
            onPress={this.onPress}
          />
        </View>
      </View>
    );
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-03-31 17:59:01

您确定"this“动词指的是正确的对象(类实例)吗?

让我们来看看这个:

代码语言:javascript
复制
class Login extends React.Component {

    const onPress = () => {
      console.log("the world is not enough");
    };

    render() {
        /* assign this class instance to that reference */
        const that = this;

        return(
            ...
              <View style={styles.buttonContainer}>
                  <AppButton
                  title="LOGIN"
                  style={styles.loginButton}

                  /* use that class reference */
                  onPress={that.onPress}
                  />
              </View>
            ... 
        );
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60956513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档