Skip to content

Commit a1dfc88

Browse files
committed
♻️ change function components
1 parent 9652eeb commit a1dfc88

File tree

5 files changed

+140
-92
lines changed

5 files changed

+140
-92
lines changed
Lines changed: 58 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,86 @@
1-
import React from "react";
1+
import React, { useEffect, useState } from "react";
22
import User from "./User";
33
import Score from "./Score";
44
import "./App.css"
55

6-
class App extends React.Component{
7-
constructor(props){
8-
super(props)
9-
this.state = {
10-
values : {
11-
0 : {
12-
value : "rock",
13-
emoji : "✊"},
14-
1 : {
15-
value : "scissor",
16-
emoji : "✌️"
17-
},
18-
2 : {
19-
value : "paper",
20-
emoji : "🖐"
21-
}},
22-
show : 0,
23-
gamer : 0,
24-
user : 0
25-
}
26-
this.checkResult = this.checkResult.bind(this)
27-
this.restart = this.restart.bind(this)
28-
this.clear = this.clear.bind(this)
6+
export default function App() {
7+
const [values, setValues] = useState({
8+
0 : {
9+
value : "rock",
10+
emoji : "✊"},
11+
1 : {
12+
value : "scissor",
13+
emoji : "✌️"
14+
},
15+
2 : {
16+
value : "paper",
17+
emoji : "🖐"
18+
}})
19+
const [show, setShow] = useState(0);
20+
const [gamer, setGamer] = useState(0)
21+
const [user, setUser] = useState(0)
22+
23+
let showValues;
24+
25+
const restart = () => {
26+
showValues = setInterval(() => {
27+
let newValue = show === 0 ? 1
28+
: show === 1 ? 2
29+
: show === 2 && 0
30+
setShow(newValue)
31+
}, 100);
2932
}
3033

31-
componentDidMount(){
32-
this.restart()
34+
35+
const clear = () => {
36+
setGamer(0)
37+
setUser(0)
3338
}
3439

35-
componentWillUnmount(){
36-
clearInterval(this.showValues);
40+
const result = (winner) => {
41+
alert(winner === "gamer" ? "졌다!!!!!" : "이겼다!!!!")
42+
winner === "gamer" ? setGamer(gamer++) : setUser(user++)
3743
}
3844

39-
checkResult(num){
40-
clearInterval(this.showValues)
41-
if (this.state.show === num) {
45+
46+
const checkResult = (num) => {
47+
clearInterval(showValues)
48+
if (show === num) {
4249
alert("비겼다.")
4350
}
44-
else if(this.state.show < 2){
45-
this.state.show+1 === num ? this.result("gamer") : this.result("user")
51+
else if(show < 2){
52+
(show+1) === num ? result("gamer") : result("user")
4653
}
4754
else{
48-
num === 0 ? this.result("gamer") : this.result("user");
55+
num === 0 ? result("gamer") : result("user");
4956
}
5057
setTimeout(() => {
51-
this.restart()
58+
restart()
5259
}, 300);
5360
}
54-
55-
56-
restart(){
57-
this.showValues = setInterval(() => {
58-
let newValue = this.state.show === 0 ? 1
59-
: this.state.show === 1 ? 2
60-
: this.state.show === 2 && 0
61-
62-
this.setState({
63-
show : newValue
64-
})
65-
}, 100);
66-
}
6761

68-
// win(){
69-
// alert("이겼다!!!!");
70-
// this.setState((prev) => {
71-
// return {user : prev.user + 1}
72-
// })
62+
useEffect(() => {
63+
restart()
64+
return clearInterval(showValues)
65+
})
7366

74-
// }
75-
76-
// lose(){
77-
// alert("졌다!!!!!!!!");
78-
// this.setState((prev) => {
79-
// return {gamer : prev.gamer + 1}
80-
// })
81-
82-
// }
83-
84-
result(winner){
85-
alert(winner === "gamer" ? "졌다!!!!!" : "이겼다!!!!")
86-
this.setState((prev) => {
87-
return {[winner] : prev[winner] + 1}
88-
})
89-
}
90-
91-
clear(){
92-
this.setState({
93-
user : 0,
94-
gamer : 0
95-
})
96-
}
97-
98-
render(){
99-
return (
100-
<>
67+
return (
68+
<>
10169
<h1><span className = "gameName">가위바위보</span> 게임을 해봅시다.</h1>
102-
<Score player = "gamer">{this.state.gamer}</Score>
70+
<Score player = "gamer">{gamer}</Score>
10371
<div className="gamerValue">
104-
{this.state.values[this.state.show].emoji}
72+
{values[show].emoji}
10573
</div>
106-
<Score player = "user">{this.state.user}</Score>
74+
<Score player = "user">{user}</Score>
10775
<div className="btns">
108-
<User onClick = {this.checkResult.bind(this, 0)}>{this.state.values[0].emoji}</User>
109-
<User onClick = {this.checkResult.bind(this, 1)}>{this.state.values[1].emoji}</User>
110-
<User onClick = {this.checkResult.bind(this, 2)}>{this.state.values[2].emoji}</User>
111-
<User onClick = {this.clear} >🔄</User>
76+
<User onClick = {() => checkResult(0)}>{values[0].emoji}</User>
77+
<User onClick = {() => checkResult(1)}>{values[1].emoji}</User>
78+
<User onClick = {() => checkResult(2)}>{values[2].emoji}</User>
79+
<User onClick = {clear} >🔄</User>
11280
</div>
113-
</>
114-
)
115-
}
116-
81+
</>
82+
)
11783
}
11884

85+
11986
export default App;

짝맞추기/minju/src/App.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import React from "react";
2+
import styled, { ThemeProvider } from "styled-components";
3+
import GlobalStyles from "./reset";
4+
import theme from "./theme";
5+
import Card from "./Card";
6+
17
function App() {
2-
return <div className="App"></div>;
8+
return (
9+
<>
10+
<GlobalStyles />
11+
<ThemeProvider theme={theme}>
12+
<Card></Card>
13+
</ThemeProvider>
14+
</>
15+
);
316
}
417

518
export default App;

짝맞추기/minju/src/Card.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import styled from "styled-components";
2+
import theme from "./theme";
3+
4+
export default function Card() {
5+
return (
6+
<CardFrame>
7+
<CardPiece>
8+
<CardFront>fff</CardFront>
9+
<CardBack>fff</CardBack>
10+
</CardPiece>
11+
</CardFrame>
12+
);
13+
}
14+
15+
const CardFrame = styled.div`
16+
width: 100px;
17+
height: 150px;
18+
perspective: 600px;
19+
`;
20+
21+
const CardPiece = styled.div`
22+
width: 100%;
23+
height: 100%;
24+
position: relative;
25+
transition: transform 1s;
26+
transform-style: preserve-3d;
27+
`;
28+
29+
const CardFace = styled.div`
30+
position: absolute;
31+
width: 100%;
32+
height: 100%;
33+
backface-visibility: hidden;
34+
`;
35+
36+
const CardFront = styled(CardFace)`
37+
background: ${theme.cardFront};
38+
`;
39+
40+
const CardBack = styled(CardFace)`
41+
background: ${theme.cardBack};
42+
transform: rotateY(180deg);
43+
`;

짝맞추기/minju/src/reset.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import styled, { createGlobalStyle } from "styled-components";
2+
3+
const GlobalStyles = createGlobalStyle`
4+
*{
5+
padding: 0;
6+
margin: 0;
7+
box-sizing: border-box;
8+
}
9+
10+
ul {
11+
list-style : none;
12+
}
13+
14+
`;
15+
16+
export default GlobalStyles;

짝맞추기/minju/src/theme.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const theme = {
2+
mainColor: "#F2CEAE",
3+
headerColor: "#A61A0D",
4+
cardBack: "#D95D30",
5+
cardFront: "#F28729",
6+
btnColor: "#4F7302",
7+
};
8+
9+
export default theme;

0 commit comments

Comments
 (0)