Не удается установить значение с помощью хука useState в reactjs [duplicate]
function App() {
const [LinkList, setLinkList] = useState([]);
const [categories, setCategories] = useState({id:[], cat:[]});
var catArray = [];
useEffect(() => {
fetch("http://127.0.0.1:8000/api/service/category-list/")
.then((response) => response.json())
.then((data) => {
for (var i = 0; i < data.length; i++) {
catArray.push({id:data[i].id, cat:data[i].category});
}
setCategories(catArray);
console.log(categories); //this line displays empty array
console.log(catArray)//this line literally displays the date fetched.
if( setCategories(catArray)){
console.log(true); //it rejects this line of code
}else{
console.log("false");//this line is executed
}
});
}, []);
Я поместил проблему в комментарий кода -->{id: Array(0), cat: Array(0)} когда я пытаюсь записать "категории" в консоль, отображается вышеуказанная строка. пожалуйста, поделитесь со мной своей идеей по этой проблеме. Код дословно правильный, но все равно выдает ошибку в консоль