0

I am trying to call a function inside a class but confused with below syntax.

Why I need to use bind method when calling a function?
Why arrow function is not getting executed?

import React, { Component } from 'react'
import { Text, View } from 'react-native'  

//arrow function not getting executed!  
myFun = data => {
  console.warn(data);
}

myFun(data){
  console.warn(data);
}

class Hello extends Component {
  render() {
    return (
      <View>
        {this.myFun("first")}
        {this.myFun.bind(this, "second")}
      </View>
    );
  }
}

export default Hello;

Note: I have removed comment from the first method!

1
  • Put those functions inside the class Commented Nov 17, 2019 at 16:16

4 Answers 4

1
  1. Put the function into the class

  2. Bind the function in the constructor:

    this.handleChange = this.handleChange.bind(this);

  3. The bind method doesn't execute the function/method, only it return the function with new context (this)

Sign up to request clarification or add additional context in comments.

Comments

1

The keyword this retains the value of the enclosing lexical context in our case the Hello class. As you can see in your example this which means Hello doesn't have myFun & yet you're trying to call that function!

So either you put the function inside the class or you change your call method to myFun("first") shortly.

Lastly, if you need to use this keyword inside your function, then you'll have to use the bind(this) method.

Comments

1

Functions should be called inside class to get them available with this context.

import React, { Component } from 'react'
import { Text, View } from 'react-native'  

class Hello extends Component {
constructor(props) {
    super(props);
}
//both normal fun and arrow fun should be inside class to get it executed!  
myFun1 = data => {
  console.warn(data);
}

myFun2(data){
  console.warn(data);
}


  render() {
    return (
      <View>
        {this.myFun("first")}
        {this.myFun2.bind(this, "second")}
      </View>
    );
  }
}

export default Hello;

When you are using this.myfun2.bind(this ) then you are adding it to current context of **this **. Basically after then only you will be able to do this.state , this.props, this.setstate etc

Also bind function returns a bounded function that, when executed later, the function will have the correct context ("this") for calling the original function. So bind function can be used when the function needs to be called later in certain events when it's useful.

2 Comments

And what is the use of bind with function call?
bind function returns a bounded function that, when executed later, the function will have the correct context ("this") for calling the original function. So bind function can be used when the function needs to be called later in certain events when it's useful.
0

you need to call the function as below.

import React, { Component } from 'react'
import { Text, View } from 'react-native'  

myFun = data => {
  console.warn(data);
}

class Hello extends Component {
  render() {
    return (
      <View>
        {()=>this.myFun("first")}
      </View>
    );
  }
}

export default Hello;

2 Comments

myFun is outside of the class, you can't use this.myFunc
My bad, put the function inside class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.