Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix on Profile Page #738

Merged
merged 3 commits into from
Jul 10, 2023
Merged

BugFix on Profile Page #738

merged 3 commits into from
Jul 10, 2023

Conversation

fiskryeziu
Copy link
Contributor

Description

This pull request fixes errors (#725) cannot read properties of null of displayName, uid. The error occurs when the user object or auth is still null or undefined, and the code tries to access the displayName & uid property.

##Changes Made
1.Added optional chaining before accessing displayName.
2.Added conditional check in useEffect to make sure auth.currentUser it's not null.
3.Removed unnecessary useEffect

  useEffect(() => {
    if (auth.currentUser) {
      setUser(auth.currentUser);
    } else {
      navigate("/dummygram/login");
    }
  }, []);

because was doing the same thing like below useEffect (which this handles much better the auth state changes)

useEffect(() => {
    const unsubscribe = auth.onAuthStateChanged((authUser) => {
      if (authUser) {
        setUser(authUser);
        setName(location?.state?.name || authUser.displayName);
        setAvatar(location?.state?.avatar || authUser.photoURL);
        setEmail(
          location?.state?.name === authUser?.displayName
            ? location?.state?.email || authUser.email
            : ""
        );
        setUid(location?.state?.uid || authUser.uid);
      } else {
        navigate("/dummygram/login");
      }
    });

    return () => {
      unsubscribe();
    };
  }, []);

4.removed profilePc & adde avatar state inside src prop to display user profile img.

Copy link
Owner

@narayan954 narayan954 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me!

@narayan954
Copy link
Owner

Description

This pull request fixes errors (#725) cannot read properties of null of displayName, uid. The error occurs when the user object or auth is still null or undefined, and the code tries to access the displayName & uid property.

##Changes Made 1.Added optional chaining before accessing displayName. 2.Added conditional check in useEffect to make sure auth.currentUser it's not null. 3.Removed unnecessary useEffect

  useEffect(() => {
    if (auth.currentUser) {
      setUser(auth.currentUser);
    } else {
      navigate("/dummygram/login");
    }
  }, []);

because was doing the same thing like below useEffect (which this handles much better the auth state changes)

useEffect(() => {
    const unsubscribe = auth.onAuthStateChanged((authUser) => {
      if (authUser) {
        setUser(authUser);
        setName(location?.state?.name || authUser.displayName);
        setAvatar(location?.state?.avatar || authUser.photoURL);
        setEmail(
          location?.state?.name === authUser?.displayName
            ? location?.state?.email || authUser.email
            : ""
        );
        setUid(location?.state?.uid || authUser.uid);
      } else {
        navigate("/dummygram/login");
      }
    });

    return () => {
      unsubscribe();
    };
  }, []);

4.removed profilePc & adde avatar state inside src prop to display user profile img.

thanks a lot for fixing these many stuffs @fiskryeziu , merging them!

@narayan954 narayan954 linked an issue Jul 10, 2023 that may be closed by this pull request
4 tasks
@narayan954 narayan954 merged commit 8d982be into narayan954:master Jul 10, 2023
4 of 6 checks passed
@fiskryeziu fiskryeziu deleted the bugfix-profilepage-issue branch July 21, 2023 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Error: Cannot read properties of null
2 participants