I’m trying to fetch and use Directus API data.
I’m doing all by textbook, and yet it won’t assign received object:
import React, { useState, useEffect } from "react";
import { createDirectus, rest, readItems } from '@directus/sdk';
const Home = () => {
const [games, setGames] = useState([]);
useEffect(() => {
fetchNewsGames();
}, []);
const fetchNewsGames = async () => {
const response = await axios.get(
"http://localhost:8055/items/stockholm_games"
);
/* tried this as well, it returns in "response", needed objects, in console.log, but still can't assign them !!! ):
const client = createDirectus('http://localhost:8055').with(rest());
const response = await client.request(readItems('stockholm_games')); */
setGames(response.data.data)
console.log("in console:", response);
console.log("saved in state is:", games);
// to get properties..
console.log("list of all: "+
games.map(item => (
item.title
))
)
};
Only at second try, it populates, only if I set
}, [games]);