I got an warning message after i created a function to set value data current user loggedin and called it in useEffect. Here's the code:
import React, { useEffect, useState } from "react";
...
const AddPost = () => {
const userStorage = sessionStorage.getItem('user');
const [values, setValues] = useState({
userId: '',
authorName: '',
title: '',
content: '',
likes: '',
dislikes: ''
});
useEffect(() => {
getCurrentUserInfo();
},[]);
const getCurrentUserInfo = () => {
if(userStorage !== null) {
setValues({
...values,
userId: userStorage.id,
authorName: userStorage.userName,
})
}
}
...
export default AddPost;
i just want to set userId and authorName from user logged in data, so in function setValues i get all initial values, then i update userId and authorName only. And suddenly there's warning message in my browser console, and it says
React Hook useEffect has a missing dependency: 'getCurrentUserInfo'. Either include it or remove the dependency array
i try to remove []
in useEffect
, but it causes my browser lag because useEffect
do looping called function.
i try to add values
and userStorage
or one of them inside []
, still not successed.
is there any solutions for this?