npx create-react-app 프로젝트 명 --template typescript
App.tsx
import "./App.css";
import Greetings from "./Greetings";
function App() {
const onClick = (name: string) => {
console.log(`${name} says hello`);
};
return <Greetings name="Hello" onClick={onClick} />;
}
export default App;
Greetings.tsx
import React from "react";
type GreetingsProps = {
name: string;
mark: string;
optional?: string;
onClick: (name: string) => void; //아무것도 리턴 안함
};
function Greetings({ name, mark, optional, onClick }: GreetingsProps) {
const handleClick = () => onClick(name);
return (
<div>
Hello, {name} {mark}
{optional && <p>{optional}</p>}
<div>
<button onClick={handleClick}>Click Me</button>
</div>
</div>
);
}
Greetings.defaultProps = {
mark: "!",
};
export default Greetings;