Here is the code to initialize categoryArray
. I want to run the useEffect
only if the categoryArray
data changes.
const [categoryArray, setCategoryArray] = useState([]);
useEffect(() => {
axios.get('http://localhost:8080/user/category')
.then(response => {
if (categoryArray !== response.data.category) {
setCategoryArray(response.data.category);
}
})
.catch(err => {
console.log(err);
})
}, [categoryArray])
I tried to apply a conditional statement to setCategoryArray
only if there is some change but somehow that conditional statement is not working properly and an infinite loop is happening.
Could you please guide me on where I am getting wrong, also let me know if more information is required.