Created
July 31, 2022 22:25
-
-
Save joaobibiano/af1220d8e661e03fbfb4bb13be879bc2 to your computer and use it in GitHub Desktop.
Código da Vídeo sobre React com typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
import "./App.css"; | |
type TypographyProps = { | |
children: React.ReactNode; | |
size?: "small" | "large"; | |
}; | |
type ParagraphProps = { | |
color: string; | |
}; | |
const Paragraph = ({ | |
children, | |
size, | |
color, | |
}: TypographyProps & ParagraphProps) => { | |
return ( | |
<h1 | |
style={{ | |
fontSize: size === "small" ? "1.5rem" : "3rem", | |
color: color, | |
}} | |
> | |
{children} | |
</h1> | |
); | |
}; | |
const Title = ({ children, size }: TypographyProps) => { | |
return ( | |
<h1 | |
style={{ | |
fontSize: size === "small" ? "1.5rem" : "3rem", | |
}} | |
> | |
{children} | |
</h1> | |
); | |
}; | |
const titleDefaultProps = { | |
size: "small", | |
}; | |
Title.defaultProps = titleDefaultProps; | |
const user = { | |
id: 1, | |
name: "John Doe", | |
age: 30, | |
isAdmin: true, | |
birthDate: new Date("1980-01-01"), | |
}; | |
type UserAttributes = typeof user; | |
function sumNumbers(a: number, b: number) { | |
return a + b; | |
} | |
function List<ItemType>({ | |
items, | |
render, | |
}: { | |
items: ItemType[]; | |
render: (item: ItemType, index: number) => React.ReactNode; | |
}) { | |
return ( | |
<ul> | |
{items.map((item, index) => { | |
return render(item, index); | |
})} | |
</ul> | |
); | |
} | |
function App() { | |
const [count, setCount] = useState<number | null>(null); | |
if (count !== null) { | |
return <div>{count}</div>; | |
} | |
const items = [ | |
{ | |
code: 1, | |
name: "John Doe", | |
}, | |
{ | |
code: 2, | |
name: "Jane Doe", | |
}, | |
]; | |
return ( | |
<div className="App"> | |
<Title> | |
<span> | |
Hello <b>World</b> | |
</span> | |
</Title> | |
<Paragraph color="red" size="small"> | |
Eu sou um parágrafo | |
</Paragraph> | |
<List | |
items={items} | |
render={(item, index) => { | |
if (item.code === 1) { | |
return <p>{item.name}</p>; | |
} | |
return <h3>{item.name}</h3>; | |
}} | |
/> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
boa!