-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.js
71 lines (64 loc) · 1.42 KB
/
Counter.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { useState } from 'react';
/** @jsxFrag React.Fragment */
/** @jsx jsx */
import { css, jsx } from '@emotion/core';
const bgColor = 'lightgray';
const Button = (props) => {
console.log(props.tooHigh);
return (
<button
css={css`
font-size: 3rem;
padding: 1.5rem;
border-radius: 8px;
background-color: ${//
// condition ? whatYouGetIfItsTrue : whatYouGetIfItsFalse
props.tooHigh === true ? 'red' : bgColor};
& + * {
margin-left: 20px;
}
`}
{...props}
/>
);
};
// Keep track of the count of the counter
export default function Counter() {
// This is similar to the following code:
// const count = 0;
const [count, setCount] = useState(0);
return (
<>
{count}
<div
css={css`
display: flex;
margin: 2rem;
`}
>
<Button
onClick={() => {
// This is like the following code
// count = count + 1;
// count++;
setCount(count + 1);
}}
tooHigh={count > 5}
>
+
</Button>
<Button
onClick={() => {
// This is like the following code
// count = count - 1;
// count--;
setCount(count - 1);
}}
tooHigh={count > 5}
>
-
</Button>
</div>
</>
);
}