You must study this commit for the sake of Mark-san
This commit is contained in:
parent
49e4015c17
commit
dc33eed88b
2 changed files with 17 additions and 3 deletions
|
@ -9,7 +9,10 @@ class PostList extends React.Component {
|
|||
this.props.fetchPosts();
|
||||
}
|
||||
renderList() {
|
||||
return this.props.posts.map(post => {
|
||||
const {posts} = this.props
|
||||
const postKeys = Object.keys(posts.entities)
|
||||
return postKeys.map(id => {
|
||||
const post = posts.entities[id]
|
||||
return (
|
||||
<div className="item" key={post.id}>
|
||||
<div className="content">
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
export default (state = [], action) => {
|
||||
const initialState = {
|
||||
entities: {},
|
||||
};
|
||||
|
||||
const normalizeEntities = (entities, payloadData) => {
|
||||
const entitiesMap = {}
|
||||
payloadData.forEach(post => entitiesMap[post.id] = post)
|
||||
return {...entities, ...entitiesMap}
|
||||
}
|
||||
|
||||
export default (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case 'FETCH_POSTS':
|
||||
return action.payload;
|
||||
const mergedEntities = normalizeEntities(state.entities, action.payload)
|
||||
return {...state, ...{entities: mergedEntities}}
|
||||
case 'FETCH_POST':
|
||||
return action.payload;
|
||||
default:
|
||||
|
|
Reference in a new issue