首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Next.js链接不强制组件呈现,serverSideProps不更新

Next.js链接不强制组件呈现,serverSideProps不更新
EN

Stack Overflow用户
提问于 2020-12-27 09:03:08
回答 1查看 85关注 0票数 1

因此,当我的用户试图通过导航栏中的链接从一个配置文件转到另一个配置文件时,我遇到了next.js的这个问题:

代码语言:javascript
复制
<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?

我的个人资料页面如下所示:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-10 08:10:52

不要担心这是一个已知的bug,你可以在https://github.com/vercel/next.js/issues/10400这里了解更多。希望这个问题很快就能解决。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65462129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档