프론트엔드 개발자는 백엔드로부터 API가 나오지 않더라도 mock data, 즉 가짜 데이터를 만들어서 미리 내가 만든 화면에서 데이터가 어떻게 나타나는지 테스트하며 개발을 진행 할 수 있습니다.
1)data.js
const COMMENT = [
{
id: 1,
userName: 'wecode',
content: 'Welcome to world best coding bootcamp!',
isLiked: true
},
{
id: 2,
userName: 'joonsikyang',
content: 'Hi there.',
isLiked: false
},
{
id: 3,
userName: 'jayPark',
content: 'Hey.',
isLiked: false
}
];
export default COMMENT;
2)Comment.js
import React, { Component } from 'react';
import CommentList from './CommentList/CommentList';
import COMMENT from './commentData';
import './Comment.scss';
class Comment extends Component {
constructor() {
super();
this.state = {
commentList: [],
commentValue: ''
};
}
componentDidMount() {
this.setState({
commentList: COMMENT
});
}
handleCommentValue = e => {
this.setState({
commentValue: e.target.value
});
};
addComment = e => {
e.preventDefault();
const { commentList, commentValue } = this.state;
this.setState({
commentList: [
...commentList,
{
id: commentList.length + 1,
userName: 'wecode',
content: commentValue,
isLiked: false
}
],
commentValue: ''
});
};
render() {
const { commentList, commentValue } = this.state;
return (
<div className="comment">
<h1>Main Page</h1>
<ul>
{commentList.map(comment => {
return (
<CommentList
clickEvent={this.changeColor}
name={comment.userName}
comment={comment.content}
/>
);
})}
</ul>
<div className="commentInput">
<form onSubmit={this.addComment}>
<input
onChange={this.handleCommentValue}
type="text"
placeholder="enter comment"
value={commentValue}
/>
</form>
<button className="addCommentBtn" onClick={this.addComment}>
Click
</button>
</div>
</div>
);
}
}
export default Comment;
{
"data": [
{
"id": 1,
"userName": "wecode",
"content": "Welcome to world best coding bootcamp!",
"isLiked": true
},
{
"id": 2,
"userName": "joonsikyang",
"content": "Hi there.",
"isLiked": false
},
{
"id": 3,
"userName": "jayPark",
"content": "Hey.",
"isLiked": false
}
]
}
해당 데이터는 http://localhost:3000/data/data.json 주소를 입력해 확인해 볼 수 있습니다.
해당 주소를 API 주소로 생각하고 fetch 함수와 http GET method 를 사용해 실제 API에서 데이터를 받아오는 것처럼 코드를 작성합니다.
App.js
import React, { Component } from 'react';
import CommentList from './CommentList/CommentList';
import './Comment.scss';
class Comment extends Component {
constructor() {
super();
this.state = {
commentList: [],
commentValue: ''
};
}
componentDidMount() {
fetch('http://localhost:3000/data/commentData.json', {
method: 'GET'
})
.then(res => res.json())
.then(res => {
this.setState({
commentList: res.data
});
});
}
render() {
const { commentList, commentValue } = this.state;
return (
<div className="comment">
<h1>Main Page</h1>
<ul>
{commentList.map(comment => {
return (
<CommentList
clickEvent={this.changeColor}
name={comment.userName}
comment={comment.content}
/>
);
})}
</ul>
<div className="commentInput">
<form onSubmit={this.addComment}>
<input
onChange={this.handleCommentValue}
type="text"
placeholder="enter comment"
value={commentValue}
/>
</form>
<button className="addCommentBtn" onClick={this.addComment}>
Click
</button>
</div>
</div>
);
}
}
export default Comment;