aachukay
aachukay

Reputation: 37

How do I call a function inside another function through onclick in my React component?

I have a function component which has an outer() function containing the inner() function. The outer() function returns the inner() function. When rendering the DOM, I'm attaching an onClick handler which needs to fire the inner() function but I'm not able to access it. How do I target it?

My code looks like this:

function Main() {
   ....
   function outer() {
      ...
      function inner() {
      ...
      }
   return inner;
   }
return (
   <button onclick={?}></button>
}

Upvotes: 2

Views: 154

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

<button onClick={() => outer()()}></button>

or

<button onClick={outer()}></button>

Upvotes: 4

Related Questions