Home > Back-end >  Uncaught ReferenceError: handleClick is not defined react native
Uncaught ReferenceError: handleClick is not defined react native

Time:07-10

Can anyone please explain to me why when I run this code I got this error : App.js:11 Uncaught ReferenceError: handleClick is not defined.the code in question

but when I changed it to the code below it workedthe code that works

CodePudding user response:

You need to move handleClick to the function component.

export default function App() {
  const handleClick = () => { ... }
  ...
}

And call it without the this keyword:

<Button onPress={() => {
  handleClick(item.link)
}}>Top to view course</Button>

CodePudding user response:

in the first case, you are using function component and this inside function component refers to the window object, so when you call the function using this.handleClick(), js looks for the function in the window object and since it doesn't find the function there it gives you a reference error

But at the same time in the class component, it works because this refers to the object, which is an instance of the class Componet

  • Related