๋๋ฏธ๋ฐ์ดํฐ์ ๋ฉ์ธํฌ์คํธ๋ฅผ ๋น๋ฐฐ์ด๋ก ๋ง๋ค์ด์ค๋ค ์๋์ ๊ฐ์ด ํด์ฃผ์ธ์
export const generateDummyPost = (number) => {
Array(number)
.fill()
.map(() => ({
id: shortId.generate(), //shortid ์ฌ์ฉ
User: {
id: shortId.generate(), //shortid ์ฌ์ฉ
nickname: faker.name.findName(), //faker์ฌ์ฉ
},
content: faker.lorem.paragraph(),
Images: [
{
src: faker.image.image(),
},
],
Comments: [
{
User: {
id: shortid.generate(),
nickname: faker.name.findName(),
},
content: faker.lorem.sentence(),
},
],
}));
};
initialState.mainPosts = initialState.mainPosts.concat(generateDummyPost(10));
//์ํ
hasMorePost: true,
loadPostsLoading: false,
loadPostsDone: false,
loadPostsError: null,
//์ก์
export const LOAD_POSTS_REQUEST = "LOAD_POSTS_REQUEST";
export const LOAD_POSTS_SUCCESS = "LOAD_POSTS_SUCCESS";
export const LOAD_POSTS_FAILURE = "LOAD_POSTS_FAILURE";
//๋ฆฌ๋์
case LOAD_POSTS_REQUEST:
draft.loadPostsLoading = true;
draft.loadPostsDone = false;
draft.loadPostsError = null;
break;
case LOAD_POSTS_SUCCESS:
draft.loadPostsLoading = false;
draft.loadPostsDone = true;
draft.loadPostsError = null;
draft.mainPosts = action.data.concat(draft.mainPosts); //์์ ์ถ๊ฐํด์ผ ๊ฒ์๊ธ์ด ์์ ๋ํ๋๋ค.
draft.hasMorePost = draft.mainPosts.length < 50;
break;
case LOAD_POSTS_FAILURE:
draft.loadPostsLoading = false;
draft.loadPostsDone = false;
draft.loadPostsError = action.error;
break;
//์ฌ๊ฐ
function loadPostAPI(data) {
const response = axios.post("/api/post");
return response.data;
}
function* loadPost(action) {
try {
yield delay(1000);
const id = shortid.generate();
// const data = yield call(loadPostAPI, action.data);
yield put({
type: LOAD_POSTS_SUCCESS,
data: data: generateDummyPost(10),
});
} catch (e) {
yield put({
type: LOAD_POSTS_FAILURE,
data: e.response.data,
});
}
}
function* watchLoadPost() {
yield takeEvery(LOAD_POSTS_REQUEST, loadPost);
}
export default function* postSaga() {
yield all([
fork(watchAddComment),
fork(watchAddPost),
fork(watchLoadPost),
fork(watchRemovePost),
]);
}
์ธ๋ฑ์ค ํ์ด์ง๋ก ๊ฐ์
useEffect(() => {
dispatch({ type: LOAD_POSTS_REQUEST });
}, [dispatch]);
์ถ๊ฐํด์ฃผ๋ฉด ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ๋ ๋ ๋ก๋๋๊ณ , ์คํฌ๋กค์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํด์ค๋๋ค
function onScroll() {
console.log(
window.scrollY, //์ผ๋ง๋ ๋ด๋ ธ๋์ง
document.documentElement.clientHeight, //ํ๋ฉด์ ๋ณด์ด๋๊ธธ์ด
document.documentElement.scrollHeight //์ด๊ธธ์ด
//์ 3๊ฐ์ง๊ฐ ๊ฐ์ฅ ๋ง์ด ์ฐ์ธ๋ค
// ์ด๊ธธ์ด = ์ผ๋ง๋ ๋ด๋ ธ๋์ง + ํ๋ฉด์ ๋ณด์ด๋๊ธธ์ด
);
}
useEffect(() => {
function onScroll() {
console.log(
window.scrollY,
document.documentElement.clientHeight,
document.documentElement.scrollHeight
);
if (
window.scrollY + document.documentElement.clientHeight ===
document.documentElement.scrollHeight
) {
dispatch({ type: LOAD_POSTS_REQUEST });
}
}
window.addEventListener("scroll", onScroll);
return () => {
window.removeEventListener("scroll", onScroll);
//๋ฆฌํดํด์ ๋ค์ ์ญ์ ์ํด์ฃผ๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ์์ด๊ฒ ๋๋ค.
};
}, []);
react virtualize
๋ชจ๋ฐ์ผ์๋ ์ปดํฌ๋ํธ๊ฐ ๋๋ฌด ๋ง์ด ๋ ํฐ๋๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ก์๋จน๊ฒ๋๋๋ฐ,
์ธ์คํ๊ทธ๋จ๊ณผ ๊ฐ์ด ๋ฆฌ์คํธ๊ฐ ๋ง์ด ๋์ค๋ ๊ฒ๋ค์ ํจ์จ์ ์ผ๋ก ํ๋ฉด์๋ 3๊ฐ์ฉ๋ง ๋ณด์ฌ์ค์,
ํ๋ฉด์์๋ ๋์ด ์์ด์ง๋๋ก ๋์์ค๋ค.
๊ฒ์ ใฑใฑ!