I fetch data and want to set state with newly received data. I have a special function for data fetching (loadData) and a component(App) where I have to setState. However, I can't pass there data from loadData. When I write const res = loadData(), I get Pending...
async function loadData() {
let query = `someQuery`;
let url = 'someUrl',
options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
query
})
};
const response = await fetch(url, options);
const body = await response.json();
return body.data // I have to set this data to state
}
const App = () => {
const [state, setState] = useState([])
useEffect(() => {
loadData(); // here I would like to setState. How to pass in here data
received as a result
of loadData?
}, [])
}