首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >找不到变量onPress React-本机

找不到变量onPress React-本机
EN

Stack Overflow用户
提问于 2019-10-14 15:33:24
回答 2查看 89关注 0票数 0

我刚开始使用react,并且我试图在按钮的onPress方法中运行一个简单的函数,但是当我试图加载按钮所在的页面时,它返回错误"unable to find variable 'test'“

以下是代码

代码语言:javascript
复制
import React from 'react';
import { Layout, Button, Text  } from 'react-native-ui-kitten';

export default class Settings extends React.Component {

test(){
      fetch('http://192.168.100.160:3000/prenotazioni')
        .then(response => response.json())
        .then(users => console.warn(users))
}

render(){
  return (
  <Layout style={{flex: 1, alignItems: 'center', justifyContent: "center"}}>
    <Text>Sei nelle Settings</Text>
    <Button onPress={test}>MySQL</Button>
  </Layout>
  );
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-14 15:39:31

请改用onPress={this.test}

票数 1
EN

Stack Overflow用户

发布于 2019-10-14 15:39:57

改为添加onPress={this.test}。如下图所示:

代码语言:javascript
复制
import React from 'react';
import { Layout, Button, Text  } from 'react-native-ui-kitten';

export default class Settings extends React.Component {

constructor(props) {
    super(props);

    // This binding is necessary to make `this` work in the callback
    this.test = this.test.bind(this);
 }

test(){
      fetch('http://192.168.100.160:3000/prenotazioni')
        .then(response => response.json())
        .then(users => console.warn(users))
}

render(){
  return (
  <Layout style={{flex: 1, alignItems: 'center', justifyContent: "center"}}>
    <Text>Sei nelle Settings</Text>
    <Button onPress={this.test}>MySQL</Button>
  </Layout>
  );
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58372012

复制
相关文章

相似问题

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