shortid
๋ฐฐ์ด๋ฐ์ดํฐ(๋๋ฏธ๋ฐ์ดํฐ)์ ์์ด๋๋ฅผ ๋ฃ์ด์ค๋ ์ ๋ง ๋๋คํ ๊ฐ์ ์ฐพ์์ค์ผ ํ๋ ๊ฒฝ์ฐ๋ค์ด ์๋๋ฐ ์ด๋ ์ ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ shortid์ด๋ค
import shortId from "shortid";
id: shortId.generate()
์์ฒ๋ผ ์ฌ์ฉํ ํ์ผ์์ ์ํฌํธํ๋ค ์ฌ์ฉํ ๋ฐ์ดํฐ์ ํธ์ถํด์ฃผ๋ฉด ๋๋ค.
๋๋ฏธ๋ฐ์ดํฐ์ ์ฌ์ฉํ๊ธฐ ์ข์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ faker์ด๋ค.
๊ฐ์ข
๋ฐ์ดํฐ๋ฅผ ์ค๋ค.
immer
๋ง์ฝ ์๋์ ๊ฐ์ ์ํ๊ฐ ์๋ค๋ฉด
mainPosts: [
{
id: 1,
User: {
id: 1,
nickname: "Conrad",
},
content: "์ฒซ๋ฒ์งธ๊ฒ์๊ธ #sssss",
Images: [
{
src:
"https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
},
{
src:
"https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
},
{
src:
"https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
},
],
Comments: [
{
User: {
nickname: "nero",
},
content: "์์ฐ",
},
{
User: {
nickname: "hero",
},
content: "์ค์์์",
},
],
},
],
๋ถ๋ณ์ฑ์ ์ ์งํด์ฃผ๊ธฐ๊ฐ ์ฝ์ง ์์์ผ์ด๋ค....
์ค์ ๋ก ๋๊ธํ๋ ๋ค๋๋ฐ ๋ถ๋ณ์ฑ์ ์ ์งํ๊ธฐ ์ํด ์๋์ ์ฝ๋๊ฐ ํ์ํ๋ค
case ADD_COMMENT_SUCCESS: {
//๋๊ธ์ ์ ๊ทผํ๊ธฐ ์ํด์๋ ๊ฒ์๊ธ์ ๋จผ์ ์ฐพ๊ณ ๊ทธ ์์ ์ฝ๋งจ์ธ ๋ฐฐ์ด์ ์ ๊ทผํด์ ์ถ๊ฐํด์ผ ํ๋๋ฐ ๋ถ๋ณ์ฑ์ ์ ์งํ๋ค.
const postIndex = state.mainPost.findIndex(
(y) => y.id === action.data.postId
);
const post = state.mainPost[postIndex];
const Comments = [dummyComment(action.data.content), ...post.Comments];
const mainPosts = [...state.mainPosts];
mainPosts[postIndex] = { ...post, Comments };
return {
...state,
addCommentLoading: false,
addCommentDone: true,
addCommentError: null,
//mainComments: [dummyComment, ...state.mainComments], //์์ ์ถ๊ฐํด์ผ ๊ฒ์๊ธ์ด ์์ ๋ํ๋๋ค.
};
}
immer๋ฅผ ์ฐ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
case ADD_COMMENT_SUCCESS: {
const post = draft.mainPosts.find((v) => v.id === action.data.postId);
post.Comments.unshift(dummyPost(action.data.content));
draft.addCommentLoading = false;
draft.addCommentDone = true;
break;
}
์ฐ์ ์ค์น
import produce from "immer";
์๋๊ฐ ๊ธฐ๋ณธ ํ์์ด๋ค.
export default function reducer(state = initialState, action) {
return produce(state,draft=>{
})
switch (action.type) {
draft๋ฅผ ์คํ
์ดํธ๋ก ๋ณด๊ณ ํธํ๊ฒ ๋ฐ๊พธ๋ฉด immer๊ฐ ์์์ ๋ฐ๊ฟ์ค๋ค.
๋จ state๋ฅผ ๊ฑด๋ค๋ฉด ์๋๊ณ draft๋ง ๋ฐ๊ฟ์ค์ผ ํ๋ค.
์๋ฅผ๋ค์ด ์๋์ ์ฝ๋๋ฅผ ๋ฐ๊ฟ์ฃผ๋ฉด
case ADD_POST_REQUEST:
return {
...state,
addPostLoading: true,
addPostDone: false,
addPostError: null,
};
case ADD_POST_REQUEST:
return:
draft.addPostLoading = true;
draft.addPostDone = false;
draft.addPostError = null;
break;
Faker
๋๋ฏธ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค๋ ์กฐ๊ธ๋ ํ์ค๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๊น์์ฃผ์๊ณ
์ํฌํธํด์ฃผ์
import faker from "faker";
initialState.mainPosts = initialState.mainPosts.concat( //concat์๋ ํญ์๋์
์!
Array(20)
.fill()
.map(() => ({
id: shortId.generate(), //shortid ์ฌ์ฉ
User: {
id: shortId.generate(), //shortid ์ฌ์ฉ
nickname: faker.name.findName(), //faker์ฌ์ฉ
},
content: faker.lorem.paragraph(),
Images: [
{
src: faker.image.imageUrl(),
},
],
Comments: [
{
User: {
id: shortid.generate(),
nickname: faker.name.findName(),
},
content: faker.lorem.sentence(),
},
],
}))
);
์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ mainPosts20๊ฐ๋ฅผ ๋ง๋ค์ด๋ฒ๋ ธ์ฃ ...
๋์ค์ ์ฑ๋ฅ ์ต์ ํ๊น์ง ์๊ฐํด๋ณธ๋ค๋ฉด ๋ช์ฒ๊ฐ์ฉ ๋ง๋ค์ด์ ํ
์คํธํด๋ ์ข์ต๋๋ค.
์์ธํ ๋ด์ฉ์ด๋ ์ข
๋ฅ๋ ๊นํ๋ธ ๊ณต์๋ฌธ์์ ๋์์์ต๋๋ค.
๋ฆฌ๋์คํดํท
๋ฆฌ๋์คํ์์ ๋ง๋ ํด์ธ๋ฐ ํธํ๊ฒ ๋ฆฌ๋์ค๋ฅผ ์์ฑํ ์ ์๋ค.
์ฝ๋๋์ ์กฐ๊ธ์ฉ ์ค์ผ ์ ์๋ค.