Showing
4 changed files
with
67 additions
and
32 deletions
... | @@ -20,35 +20,25 @@ const styles = theme => ({ | ... | @@ -20,35 +20,25 @@ const styles = theme => ({ |
20 | } | 20 | } |
21 | }) | 21 | }) |
22 | 22 | ||
23 | -const customers =[ | 23 | +class App extends Component { |
24 | - { | 24 | + state = { |
25 | - 'id': 1, | 25 | + customers: "" |
26 | - 'image': 'https://placeimg.com/64/64/any', | 26 | + } |
27 | - 'name': '이혜연', | 27 | + |
28 | - 'birthday': '000416', | 28 | + //모든 컴포넌트 준비 완료일 때 실행 -- react가 라이브러리이기 때문 ! |
29 | - 'gender': '여자', | 29 | + componentDidMount() { |
30 | - 'job': '대학생' | 30 | + this.callApi() |
31 | -}, | 31 | + .then(res => this.setState({customers: res})) |
32 | -{ | 32 | + .catch(err => console.log(err)); |
33 | - 'id': 2, | 33 | + } |
34 | - 'image': 'https://placeimg.com/64/64/2', | ||
35 | - 'name': '김다은', | ||
36 | - 'birthday': '000101', | ||
37 | - 'gender': '여자', | ||
38 | - 'job': '재수생' | ||
39 | -}, | ||
40 | -{ | ||
41 | - 'id': 3, | ||
42 | - 'image': 'https://placeimg.com/64/64/3', | ||
43 | - 'name': '김석희', | ||
44 | - 'birthday': '900103', | ||
45 | - 'gender': '남자', | ||
46 | - 'job': '회사원' | ||
47 | -}, | ||
48 | 34 | ||
49 | -] | 35 | + callApi = async() => { |
36 | + //local의/api/customers에 접속해 response로 받아오고 이를 body변수에 담아주겠다 | ||
37 | + const response = await fetch('/api/customers'); | ||
38 | + const body = await response.json(); | ||
39 | + return body; | ||
40 | + } | ||
50 | 41 | ||
51 | -class App extends Component { | ||
52 | render() { | 42 | render() { |
53 | const { classes } = this.props; | 43 | const { classes } = this.props; |
54 | return ( | 44 | return ( |
... | @@ -66,7 +56,9 @@ class App extends Component { | ... | @@ -66,7 +56,9 @@ class App extends Component { |
66 | </TableHead> | 56 | </TableHead> |
67 | 57 | ||
68 | <TableBody> | 58 | <TableBody> |
69 | - {customers.map(c=> { return (<Customer key={c.id} id={c.id} image={c.image} name={c.name} birthday={c.birthday} gender={c.gender} job={c.job} />)})} | 59 | + {this.state.customers ? this.state.customers.map (c=> { |
60 | + return (<Customer key={c.id} id={c.id} image={c.image} name={c.name} birthday={c.birthday} gender={c.gender} job={c.job} />) | ||
61 | + }) : ""} | ||
70 | </TableBody> | 62 | </TableBody> |
71 | </Table> | 63 | </Table> |
72 | </Paper> | 64 | </Paper> | ... | ... |
1 | +{ | ||
2 | + "name" : "management", | ||
3 | + "version": "1.0.0", | ||
4 | + "scripts":{ | ||
5 | + "client": "cd client && yarn start", | ||
6 | + "server": "node server.js", | ||
7 | + "dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\"" | ||
8 | + }, | ||
9 | + "dependencies": { | ||
10 | + "body-parser": "^1.18.3", | ||
11 | + "express": "^4.16.4" | ||
12 | + }, | ||
13 | + "devDependencies": { | ||
14 | + "concurrently":"^" | ||
15 | + } | ||
16 | +} | ... | ... |
... | @@ -6,9 +6,35 @@ const port = process.env.PORT || 5000; | ... | @@ -6,9 +6,35 @@ const port = process.env.PORT || 5000; |
6 | app.use(bodyParser.json()); | 6 | app.use(bodyParser.json()); |
7 | app.use(bodyParser.urlencoded({ exrended : true })); | 7 | app.use(bodyParser.urlencoded({ exrended : true })); |
8 | 8 | ||
9 | -//api의 hello라는 경로로 들어갔을때의 처리 | 9 | +app.get('/api/customers', (req, res) => { |
10 | -app.get('/api/hello', (req,res)=>{ | 10 | + res.send([ |
11 | - res.send({message: 'Hello Express!'}); | 11 | + { |
12 | + 'id': 1, | ||
13 | + 'image': 'https://placeimg.com/64/64/any', | ||
14 | + 'name': '이혜연', | ||
15 | + 'birthday': '000416', | ||
16 | + 'gender': '여자', | ||
17 | + 'job': '대학생' | ||
18 | + }, | ||
19 | + { | ||
20 | + 'id': 2, | ||
21 | + 'image': 'https://placeimg.com/64/64/2', | ||
22 | + 'name': '김다은', | ||
23 | + 'birthday': '000101', | ||
24 | + 'gender': '여자', | ||
25 | + 'job': '재수생' | ||
26 | + }, | ||
27 | + { | ||
28 | + 'id': 3, | ||
29 | + 'image': 'https://placeimg.com/64/64/3', | ||
30 | + 'name': '김석희', | ||
31 | + 'birthday': '900103', | ||
32 | + 'gender': '남자', | ||
33 | + 'job': '회사원' | ||
34 | + } | ||
35 | + | ||
36 | + ]) | ||
37 | + | ||
12 | }) | 38 | }) |
13 | 39 | ||
14 | app.listen(port, () => console.log(`Listening on port ${port}`)); | 40 | app.listen(port, () => console.log(`Listening on port ${port}`)); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment