redux - Normalizr create result for single enity -
i using normalizr have same response shape different api endpoints.
const post = new schema('posts'); const posts = arrayof('post'); const listresponse = [ {id: 1, text: 'post one'}, {id: 2, text: 'post two'}, ]; normalize(listresponse, posts); /* { entities: { posts: { 1: {id: 1, text: 'post one'}, 2: {id: 2, text: 'post two'} } }, result: [1, 2] } */ const singleresponse = {id: 1, text: 'post one'}; normalize(singleresponse, post); /* { entities: { posts: { 1: {id: 1, text: 'post one'} } }, result: 1 } */
then treat normalized response no matter how came.
but thing single item getting result: 1
instead of array result: [1]
, cause issues in later code.
now have normalize result
array manually, maybe there better way that?
in application, used 2 different actions case, fetch_post
, fetch_posts
accordingly.
but if have issues it, can use little hack:
const singleresponse = {id: 1, text: 'post one'}; normalize([singleresponse], posts);
when normalizing single post item, can wrap array , normalize array of posts.
Comments
Post a Comment