代码之家  ›  专栏  ›  技术社区  ›  David McNamee

react本机计数器,函数未定义

  •  1
  • David McNamee  · 技术社区  · 6 年前

    我已经从React Native的快速入门指南中稍微修改了默认的app.js,我试着做一个计数器,每当我点击屏幕中间的按钮时,这个计数器就会从1开始计数。当我运行以下代码时,我得到这个错误: warning:failed prop type:the prop‘onpress’is marked as required in‘button’,but its value is‘undefined’…… The problem is that i t does not recognize this.onbuttonPress(). inside the function renderbutton=()=>. 。我认为关键字 this 可能有问题,因为我听说它指向父函数而不是父对象,但我相信这仍然有效…我不知道为什么没有。任何帮助都会被感激的,谢谢!

    import react,component from'react';
    导入{
    平台,
    样式表,
    文本,
    视图,
    按钮
    }来自'react native';
    
    const instructions=平台。选择({
    ios:'按Cmd+R重新加载,\n'+
    'Cmd+D or shake for dev menu',
    android:'双击键盘上的r重新加载,\n'+
    '摇动或按下开发菜单的菜单按钮',
    })(二)
    
    导出默认类应用程序扩展组件<props>。{
    
    建造师(道具){
    超级(道具);
    本州={
    计数:1
    }(二)
    }
    
    renderButton=()=>。{
    返回(
    <按钮
    onPress=this.onButtonPress()
    title=“按钮”
    颜色=“841584”
    />
    )(二)
    }
    
    onButtonPress=()=>。{
    var previous=this.state.count;
    this.state.count=上一个+1;
    }
    
    呈现=()=>。{
    返回(
    <view style=styles.container>
    this.renderButton()
    <text style=styles.welcome>
    计数:this.state.count_
    </text>
    <text style=styles.instructions>
    要开始,请编辑app.js
    </text>
    <text style=styles.instructions>
    说明
    </text>
    </View>
    )(二)
    }
    }
    
    const styles=样式表。创建({
    容器:{
    弹性:1,
    justifycontent:'中心',
    Alignitems:'居中',
    背景色:“f5fcff”,
    },请
    欢迎:{
    字体大小:20,
    textAlign:'居中',
    利润率:10,
    },请
    说明:{
    textAlign:'居中',
    颜色:333333',
    边缘底部:5,
    },请
    });

    .Warning: Failed prop type: The prop 'onPress' is marked as required in 'Button', but its value is 'undefined'....问题是它无法识别this.onButtonPress()在函数内部renderButton = () => {}.我想可能是关键字有问题this,因为我听说这指向父函数而不是父对象,但我相信这仍然有效…我不知道为什么没有。任何帮助都会被感激的,谢谢!

    import React, { Component } from 'react';
    import {
      Platform,
      StyleSheet,
      Text,
      View,
      Button
    } from 'react-native';
    
    const instructions = Platform.select({
      ios: 'Press Cmd+R to reload,\n' +
        'Cmd+D or shake for dev menu',
      android: 'Double tap R on your keyboard to reload,\n' +
        'Shake or press menu button for dev menu',
    });
    
    export default class App extends Component<Props> {
      
      constructor(props) {
        super(props);
        this.state = {
          count: 1
        };
      }
    
      renderButton = () => {
        return(
          <Button
            onPress={this.onButtonPress()}
            title="BUTTON"
            color="#841584"
          />
        );
      }
    
      onButtonPress = () => {
        var previous = this.state.count;
        this.state.count = previous + 1;
      }
      
      render = () => {
        return (
          <View style={styles.container}>
            {this.renderButton()}
            <Text style={styles.welcome}>
              Count: {this.state.count}
            </Text>
            <Text style={styles.instructions}>
              To get started, edit App.js
            </Text>
            <Text style={styles.instructions}>
              {instructions}
            </Text>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
      },
      instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
      },
    });

    2 回复  |  直到 6 年前
        1
  •  1
  •   mccambridge    6 年前

    您正在调用函数而不是传递引用。

    renderButton = () => {
        return(
            <Button
                onPress={this.onButtonPress} // <-- you were invoking the function here instead of passing the reference
                title="BUTTON"
                color="#841584"
            />
        );
    }
    

    renderButton = () => {
        return(
            <Button
                onPress={() => this.onButtonPress()}
                title="BUTTON"
                color="#841584"
            />
        );
    }
    

    undefined 是因为函数执行没有返回值,因此返回 onPress 事件。所以react使用函数的结果作为 事件处理程序而不是函数本身。

        2
  •  1
  •   JonnyBeoulve    6 年前

    onButtonPress = () => {
        var newCount = this.state.count + 1;
         this.setState({
          count: newCount
        });
      }