type GreetingProps = {
name: string;
}
const Greeting = (props: GreetingProps) => (
<div>Hello: {props.name}</div>
)
const App = () => (
<div>
<Greeting name="Tomas" />
<Greeting name="Valeria" />
</div>
)
render(<App />);
body {
padding: 16px;
font-size: 40px;
}
type GreetingProps = {
name: string;
}
const Greeting = (props: GreetingProps) => (
<div>Hello: {props.name}</div>
)
const App = () => (
<div>
<Greeting name="Tomas" />
<Greeting name="Valeria" />
</div>
)
render(<App />);
body {
padding: 16px;
font-size: 40px;
}
type GreetingProps = {
name: string;
}
const Greeting = (props: GreetingProps) => (
<div>Hello: {props.name}</div>
)
const App = () => (
<div>
<Greeting name="Tomas" />
<Greeting name="Valeria" />
</div>
)
render(<App />);
body {
padding: 16px;
font-size: 40px;
}
type GroupProps = {
name: string;
children: React.ReactNode;
}
const Group = (props: GreetingProps) => (
<fieldset>
<legend>{props.name}</legend>
{props.children}
</fieldset>
)
const App = () => (
<div>
<Group name="To DO">
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</Group>
<Group name="Done">
<ul>
<li><s>Item 1</s></li>
<li><s>Item 2</s></li>
</ul>
</Group>
</div>
)
render(<App />);
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}
const [a, b] = [1,2];
const { a, b, ...rest } = { a: 1, b: 2, c: 3 };
type Props = { name: string, age: number }
// no destructuring
const Greeting = (props) => (
<div>
Hello { props.name }, age { props.age }
</div>
)
// destructuring
const Greeting = ({ name, ...rest }) => (
<div>
Hello { name }, age { rest.age }
</div>
)
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}
type Props = {
name: string;
age?: number;
}
const Greeting = ({ name, age = 44 }) => (
<div>
Hello { name }, your age { age }
</div>
)
const App = () => (
<>
<Greeting name="Tomas" />
<Greeting name="Valeria" age={30} />
</>
)
render(<App />)
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}
type Props = {
name: string;
age: number;
showAge: boolean;
}
const App = () => (
<>
<Greeting
name="Tomas"
age={60}
showAge
/>
<Greeting
name="Valeria"
age={30}
showAge={false}
/>
</>
)
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}
const Greeting = (props) => (
<div>
<h1>Greeting</h1>
<InnerGreeting showAge {...props} />
</div>
)
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}
const Greeting1 = (props) => {
props.name = "Tomas"; // NO!
return (
<div>
{props.name}
</div>
)
}
const Greeting2 = ({ name }) => {
name = "Tomas"; // NO!
return (
<div>
{name}
</div>
)
}
render(<Greeting1 />)
body {
padding: 16px;
font-size: 40px;
}
legend {
font-weight: bold;
}
fieldset {
padding: 16px;
font-family: monospace;
}
ul {
list-style-type: square;
padding-left: 30px;
}