因此,当我的用户试图通过导航栏中的链接从一个配置文件转到另一个配置文件时,我遇到了next.js的这个问题:
<li>
<Link href={`/profile/${user.user.id}`}>
<a className="flex flex-row items-center">
<BiUserCircle />
<span className="ml-1">Profile</span>
</a>
</Link>
</li>Next似乎没有重新呈现配置文件组件,这很不幸,因为我在getServerSideProps中拉取初始配置文件数据,这导致了奇怪的行为,比如从上次访问的配置文件中保存初始useState数据。如何确保每次用户访问配置文件页面时,都会将全新的初始数据发送到useState?
我的个人资料页面如下所示:
const Profile = ({ initialProfile, posts }) => {
const router = useRouter();
const [profile, setProfile] = useState(initialProfile);
const { userId } = router.query;
const dispatch = useDispatch();
useEffect(() => {
// This is my current solution, however it feels really hacky
fetchProfile();
}, [userId]);
const fetchProfile = async () => {
const resp = await axios.get(apiURL + `accounts/users/${userId}`);
setProfile((prof) => ({ ...prof, ...resp.data }));
};
return (
<Layout>
<ProfileSummary
isUser={isUser}
isFollowed={isFollowed}
handleFollow={handleFollow}
profile={profile}
/>
{isUser ? (
<div className="flex justify-center">
<Button colorScheme="green">Add post</Button>
</div>
) : null}
<div className="w-full flex justify-center flex-col items-center pl-2 pr-2">
{posts
? posts.map((post) => (
<Card
key={post.id}
author={post.author}
likes={post.likes}
desc={post.description}
img={post.image}
isLiked={post.is_liked}
postId={post.id}
comments={post.comments}
/>
))
: "No Posts found"}
</div>
</Layout>
);
};
export async function getServerSideProps(context) {
const { userId } = context.query;
const profileResp = await axios.get(apiURL + `accounts/users/${userId}`);
const postsResp = await axios.get(
apiURL + `posts/?author__id=${profile.id}`
);
const profile = profileResp.data;
const posts = postsResp.data;
return {
props: {
initialProfile: profile,
posts,
},
};
}
export default Profile;我真的很想得到任何帮助,也许我应该改变我的整体方法?
整个项目可以在这里找到:https://github.com/MaciejWiatr/Nextagram/tree/develop
发布于 2021-01-10 08:10:52
不要担心这是一个已知的bug,你可以在https://github.com/vercel/next.js/issues/10400这里了解更多。希望这个问题很快就能解决。
https://stackoverflow.com/questions/65462129
复制相似问题