Spaces:
Running
Running
File size: 752 Bytes
501b0f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import React, { useState } from 'react';
// Define the types for the props
interface MyComponentProps {
title: string;
subtitle?: string; // Optional prop
}
const MyComponent: React.FC<MyComponentProps> = ({ title, subtitle }) => {
// Define a state variable with an initial value
const [count, setCount] = useState<number>(0);
// Function to handle button click
const handleButtonClick = () => {
setCount(count + 1);
};
return (
<div style={{ padding: '20px', border: '1px solid #ccc', borderRadius: '8px' }}>
<h1>{title}</h1>
{subtitle && <h2>{subtitle}</h2>}
<p>Current count: {count}</p>
<button onClick={handleButtonClick}>Increase count</button>
</div>
);
};
export default MyComponent;
|