how to properly use useEffect?
Unanswered
Selkirk Rex posted this in #help-forum
Selkirk RexOP
So right now i'm using appwrite with next js.
I don't know why it doesn't show up instantly. Am i doing something wrong? I know my code is not that organized but a few tips would be nice.
I don't know why it doesn't show up instantly. Am i doing something wrong? I know my code is not that organized but a few tips would be nice.
10 Replies
Selkirk RexOP
not sure why its printing 6-12 times as well
suppose to be one only
This is for the sidebar, the "Controls" shows up late until the console prints "User is an admin" 5 times before actually showing it
It looks messy I know but it would be nice to teach on how to organize this type of stuffs
also did this on a layout this time because I am gate keeping it for admin users only
I'll come back to this tomorrow hoping someone answers
Northern Inuit Dog
How many menu items do you have? Dev might render twice (might need to google). If you need current user in the entire app why not fetch it in parent as a server component and then pass as props.
@Selkirk Rex This is for the sidebar, the "Controls" shows up late until the console prints "User is an admin" 5 times before actually showing it
I first would change the
hooks value is boolean, so it would be convenient to name it
Move the
userAdmin state:const [isUerAdmin, setIsUserAdmin] = useState<boolean>(false);hooks value is boolean, so it would be convenient to name it
isUserAdmin. there is no need for null either, have it false by default and then change it to the true. (if you don't need null in different logic)Move the
fetchUser outside the useEffect and replace fetchUser() with await fetchUser()inside the
fetchUser, you are setting userAdmin to null if error occurs, and I don't think your loading logic would work. use different state to determine loading state