I have a react app like so:
import React, { useState } from 'react';
import axios from 'axios';
const Body = () => {
const defaultVersions = [
{
version: '2.96.0',
status: null,
}
];
const [versions, setVersions] = useState(defaultVersions);
const getData = version => {
axios.get(`https://cdn10/${version}/en-US/test.js`)
.then(response => {
console.log(response.status);
return response.status;
})
.catch(err => {
console.log('in here');
})
};
const onClick = () => {
setVersions(versions.map(item => {
const { version } = item;
return { version, status: getData(version) };
}));
};
return (
<>
<button onClick={onClick}>Check Versions</button>
<ul>
{versions.map(item => {
const { version, status } = item;
return <li>{`${version}: ${status}`}</li>
})}
</ul>
</>
);
}
export default Body;
And the console.log
in getData
works, but when I try to use getData
to set the new value in my state on the button click, it's returning undefined
. I've tried making getData
async/await, but that didn't work. Any insight is appreciated!