-> Backend 와의 소통이 효율적으로 이루어질 수 있다는 장점
const COMMENT = [
{
id: 1,
userName: 'wecode',
content: 'Welcome to world best coding bootcamp!',
isLiked: true
}
];
export default COMMENT;
import React, { Component } from 'react';
import COMMENT from './commentData';
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>
);
}
}
export default Comment;
{
"data": [
{
"id": 1,
"userName": "wecode",
"content": "Welcome to world best coding bootcamp!",
"isLiked": true
}
]
}
import React, { Component } from 'react';
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>
);
}
}
export default Comment;