简体   繁体   English

如何在react-native上的函数中调用函数?

[英]How to call function in a function on react-native?

Hi this is my first time developing apps in javascript and react-native, its kind of a noob question. 嗨,这是我第一次用javascript和react-native开发应用程序,这是一个noob问题。 How do I call _getData function in __onRegionChangeComplete function? 如何调用_getData在功能__onRegionChangeComplete功能? I tried this._getData() it shows 我试过了this._getData()它显示了

error: undefined is not a function(evaluation'this._getData()')'). 错误:undefined不是函数(evaluation'this._getData()')')。

var React = require('react-native');

var {
  StyleSheet,
  Text,
  View,
  Image,
  MapView,
  Component,
} = React;

class Map extends Component {
  render() {
    return (
      <View style={styles.container}>
        <MapView style={styles.map} 
          showsUserLocation={true}
          onRegionChangeComplete={this._onRegionChangeComplete}
        />
      </View>
    );
  }

  _onRegionChangeComplete(region)
  {

  }

  _getData()
  {

  }
}

var styles = StyleSheet.create({
  container:{
    flex: 1
  },
  map: {
    flex: 1,
  },
  image: {
    width: 64,
    height: 64
  }
});

module.exports = Map;

Let me show you an example, and hope it helps you. 让我给你举个例子,希望它对你有所帮助。

1.Of course, you can use ES5 feature(createClass) instead of ES6(extends class) method to solve this binding problem. 1.当然,您可以使用ES5功能(createClass)代替ES6(扩展类)方法来解决此绑定问题。

2.You can keep using ES6, just be careful with binding this. 2.你可以继续使用ES6,只需要小心绑定它。 For example in my component 例如在我的组件中

_func1(){
    ...
}

_func2(){
  this._func1()
}

if I want to call _func1() in func2, 'this' is binded to _func2 itself, of course, there is no func1() there. 如果我想在func2中调用_func1(),'this'绑定到_func2本身,当然,那里没有func1()。

But if I bind _func2() to component context when I call _func2(), problem will be solved. 但是如果我在调用_func2()时将_func2()绑定到组件上下文,问题就会解决。

<subComponent subProp = {this._func2.bind(this)} />

Hope it helps you. 希望它能帮到你。

Solve it by changing the extends Component to createclass and adding commas after each function. 通过将extends Component更改为createclass并在每个函数后添加逗号来解决此问题。 read a brief introduction about them is that createclass have autobinding for function and extends component does not do that for you, so you have to bind them yourself. 阅读有关它们的简要介绍是createclass具有自动绑定功能,而扩展组件不会为您执行此操作,因此您必须自己绑定它们。

var React = require('react-native');

var {
  StyleSheet,
  Text,
  View,
  Image,
  MapView,
  Component,
} = React;

var Map = React.createClass({
  render(){
    return (
      <View style={styles.container}>
        <MapView style={styles.map} 
          showsUserLocation={true}
          rotateEnabled={false}
          onRegionChangeComplete={this.onRegionChangeComplete}
        />
      </View>
    );
  },

  onRegionChangeComplete(region) {
    this.getData(region)
  },

  getData(region) {

  },
});

var styles = StyleSheet.create({
  container:{
    flex: 1
  },
  map: {
    flex: 1,
  },
  image: {
    width: 64,
    height: 64
  }
});

module.exports = Map;

This example sure helpful 这个例子肯定有帮助

export default class Setup extends Component {
  _onPressButton() {
    Alert.alert('You tapped the button!')
  }

  render() {
    return (
      <View style={styles.container}>
      <View>
        <Text h1>Login</Text>
        </View>
        <View>
        <Button
          onPress={this._onPressButton}
          title="Learn More"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
        />
        </View>
      </View>
    );
  }
}

One grammar note you should address that might be causing errors: add a comma after the close brace for every method in the class. 你应该解决一个可能导致错误的语法注释:在类的每个方法的close括号后面添加一个逗号。 Try that and edit your post to see if any errors. 尝试并编辑您的帖子,看看是否有任何错误。 (Sorry I'd put this in a comment but don't have enough reputation!) (对不起,我把它放在评论中,但没有足够的声誉!)

You have to use the ES6 way of doing a function or it will not work, specially for higher version such as 0.59. 您必须使用ES6方式执行某项功能,否则它将无法工作,特别是对于更高版本(如0.59)。 The code below should work, when calling functions within class. 在类中调用函数时,下面的代码应该可以工作。 You have got it right for calling the function by using this._onRegionChangeComplete, 你已经通过使用this._onRegionChangeComplete来调用函数了,

  constructor(props) { 
  super(props); 
  this.state = {sliderValue: 15,}
  }

  var Map = React.createClass({
     render(){
     return (
     <View style={styles.container}>
        <MapView style={styles.map} 
         showsUserLocation={true}
         rotateEnabled={false}
         onRegionChangeComplete={this._onRegionChangeComplete}
    />
  </View>
    );
 },
  _onRegionChangeComplete=()=>
    {
        //Your code here. you can use this.variable if you want to use variables from 
        //your constructor(props) { super(props); this.state = {sliderValue: 15,} }

       //Example passing variable
       let Myvalue = this.state.sliderValue;

     }

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

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