Counter.tsx
import {useState} from "react"; function Counter(props: CounterProp) { const [count, setCount] = useState(0) const buttonClicked = () => { setCount(prevState => { var result = prevState + 1; if (props.onCounterChanged != null) { props.onCounterChanged(result); } return result; }) }; return ( <div> <div>Counter Value : {count}</div> <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={buttonClicked}>Next </button> </div> ); } export interface CounterProp { onCounterChanged?: (value: number) => void; } export default Counter;
App.tsx
import React from 'react'; import Counter from "./components/Counter"; function App() { return ( <div className={"px-1"}> <Counter onCounterChanged={(value => console.log(value))}></Counter> </div> ); } export default App;
References
https://www.tutorialsteacher.com/typescript/typescript-interface
https://stackoverflow.com/questions/39713349/make-all-properties-within-a-typescript-interface-optional