简体   繁体   English

Next.js 链接不强制组件渲染,serverSideProps 不更新

[英]Next.js Link doesn't enforce component render, serverSideProps doesn't get updated

so i have this issue with next.js, When my user tries to go from one profile to another via Link in navbar:所以我对 next.js 有这个问题,当我的用户尝试通过导航栏中的链接从一个配置文件到另一个配置文件 go 时:

<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 doesn't seem to re-render the profile component which is unfortunate because I'm pulling initial profile data in getServerSideProps which leads to weird behavior like when the initial useState data is saved from last visited profile. Next 似乎没有重新渲染配置文件组件,这是不幸的,因为我在 getServerSideProps 中提取初始配置文件数据,这会导致奇怪的行为,例如从上次访问的配置文件中保存初始 useState 数据时。 How can I ensure that each time user visits the profile page brand new initial data is sent to useState?如何确保每次用户访问个人资料页面时都会将全新的初始数据发送到 useState?

My profile page looks like this:我的个人资料页面如下所示:

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;

I'd really love any help, maybe I should change my overall approach?我真的很想得到任何帮助,也许我应该改变我的整体方法?

The whole project can be found here: https://github.com/MaciejWiatr/Nextagram/tree/develop整个项目可以在这里找到: https://github.com/MaciejWiatr/Nextagram/tree/develop

Dont worry this is an known bug, you can read more about it here https://github.com/vercel/next.js/issues/10400 .不用担心这是一个已知错误,您可以在此处阅读更多信息https://github.com/vercel/next.js/issues/10400 Hopefully it will be fixed soon.希望它会很快得到修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM