21.03.02 12:12 작성
·
329
0
안녕하세요 댓글 삭제 기능을 추가한뒤 테스트할때 새로고침을 안하면 화면 갱신이 안되더라구여
아래 댓글에서 뮤테이션을 적용하면 된다고 하셨는데 문서를 봐도 잘몰겠어서여 어떻게 고쳐야 되는지 알려주시면 감사여
profile.js
import AppLayout from "../components/AppLayout";
import Head from "next/head";
import NicknameEditForm from '../components/NicknameEditForm';
import FollowList from '../components/FollowList';
import React, { useEffect, useState, useCallback } from "react";
import Router from 'next/router';
import { useSelector, useDispatch } from 'react-redux';
import { LOAD_USER_REQUEST } from '../reducers/user';
import wrapper from '../store/configureStore';
import axios from 'axios';
import { END } from 'redux-saga';
import useSWR from 'swr';
const fetcher = (url) => axios.get(url, { withCredentials: true }).then((result) => result.data);
const Profile = () => {
const { me } = useSelector((state) => state.user);
const [followingsLimit, setFollowingsLimit] = useState(3);
const [followersLimit, setFollowersLimit] = useState(3);
const { data: followingsData, error: followingError } = useSWR(`http://127.0.0.1:3065/user/followings?limit=${followingsLimit}`, fetcher);
const { data: followersData, error: followerError } = useSWR(`http://127.0.0.1:3065/user/followers?limit=${followersLimit}`, fetcher);
console.log("followingsData : ", followingsData);
console.log("followersData : ", followersData);
const loadMoreFollowers = useCallback(() => {
setFollowersLimit((prev) => prev + 3);
}, []);
const loadMoreFollowings = useCallback(() => {
setFollowingsLimit((prev) => prev + 3);
}, []);
useEffect(() => {
if (!(me && me.id)) {
Router.push('/');
}
}, [me && me.id]);
if (!me) {
return '내 정보 로딩중';
}
if (followingError || followerError) {
console.error(followerError || followingError);
return '팔로잉/팔로워 로딩 중 에러가 발생했습니다.';
}
if (!followingsData || !followersData) {
return '팔로잉 데이터 로딩중...';
}
return (
<>
<Head>
<title>내 프로필 | NodeBird</title>
</Head>
<AppLayout>
<NicknameEditForm />
<FollowList header="팔로잉" data={followingsData} onClickMore={loadMoreFollowings} loading={!followingError && !followingsData} />
<FollowList header="팔로워" data={followersData} onClickMore={loadMoreFollowers} loading={!followerError && !followersData} />
</AppLayout>
</>
);
};
export const getServerSideProps = wrapper.getServerSideProps(async (context) => {
console.log(context.req.headers);
const cookie = context.req ? context.req.headers.cookie : '';
axios.defaults.headers.Cookie = '';
if (context.req && cookie) {
axios.defaults.headers.Cookie = cookie;
}
context.store.dispatch({
type: LOAD_USER_REQUEST,
});
context.store.dispatch(END);
await context.store.sagaTask.toPromise();
});
export default Profile;
import React, { useState } from "react";
import PropTypes from "prop-types";
import { List, Card, Button } from "antd";
import { StopOutlined } from "@ant-design/icons";
import { useSelector, useDispatch } from 'react-redux';
import { UNFOLLOW_REQUEST, REMOVE_FOLLOWER_REQUEST } from '../reducers/user';
const FollowList = ({ header, data, onClickMore, loading }) => {
const dispatch = useDispatch();
if (!data) {
return "데이터 로딩중";
}
const onCancel = (id) => () => {
if (header === '팔로잉') {
dispatch({
type: UNFOLLOW_REQUEST,
data: id,
});
}else{
dispatch({
type: REMOVE_FOLLOWER_REQUEST,
data: id,
});
}
};
console.log("data : ", data);
return (
<>
<h4>{header}</h4>
<div style={{ display: "flex", justifyContent: "space-between", flexWrap: "wrap" }}>
{data && data.map((v) => {
return (
<Card style={{ width: "30%" }} actions={[<StopOutlined key="stop" />]} onClick={onCancel(v.id)}>
<Card.Meta description={v.nickname} />
</Card>
)
})}
<Button block loading={loading} onClick={onClickMore}>더보기</Button>
<br /><br />
</div>
</>
);
};
FollowList.propTypes = {
};
export default FollowList;
답변 2
1
2021. 03. 02. 13:49
댓글 삭제요? 팔로잉 취소를 말씀하시는 건가요? FollowingList에 다음과 같이 mutate 추가한 후 dispatch 아래에 적으시면 됩니다. 문법 틀린 것은 수정해서 쓰세요.
const { data: followingsData, error: followingError, mutate: mutateFollowing } = useSWR(`http://127.0.0.1:3065/user/followings?limit=${followingsLimit}`, fetcher); const { data: followersData, error: followerError
, mutate: mutateFollower } = useSWR(`http://127.0.0.1:3065/user/followers?limit=${followersLimit}`, fetcher);
const onCancel = (id) => () => {
if (header === '팔로잉') {
dispatch({
type: UNFOLLOW_REQUEST,
data: id,
});
mutateFollowing((prev) => prev.filter((data) => data.id !== id));
}else{
dispatch({
type: REMOVE_FOLLOWER_REQUEST,
data: id,
});
mutateFollower((prev) => prev.filter((data) => data.id !== id));
}
};
0
2021. 03. 03. 19:34
일단 컴퍼넌트내에 아래의 두줄을 추가하면 되긴 되더라구여 그런데 이게 정확한 방법이 아니고 알려주신게 정확한 방법 같아서 적용해 봤는데 약간 에러가 발생해서 일단 아래 두줄로 처리하고 나중에 다시 한번 바꿔봐야 될거 같아여 감사합니다.