0

Consider the following code

import React, { useState } from 'react';


class MyComponent extends React.Component {
  componentDidUpdate(){
      // I want to call myClosure
  }

  render() {
    return <Counter/>;
  }
}


function Counter() {
  const [count, setCount] = useState(0);

  function myClosure(){
     console.log('Closure called');
  }

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

In the code Counter is a functional component and has a myClosure closure, How can I call myClosure in MyComponent ? As functional component has no ref and I think forwardRef cannot help me!

WebMaster
  • 3,050
  • 4
  • 25
  • 77

0 Answers0