I am trying to update the react state in functional component using API, However somehow it's not getting updated. I am not sure what is missing from my side. Can someone help what is missing in the below code snippet.
code
export default function Test(props) {
const [state, setState] = useState({
data: []
});
useEffect(() => {
getConnectionUsers("Test")
return () => {alert("component unmount");}
}, [])
}
function getConnectionUsers(connection) {
axios.get(URL).then((response) => {
if (response.status === 200) {
console.log(response.data); // This is showing correct data which is coming from API
setState({...state ,data:response.data});
console.log(state.data); //Empty State is coming which means somehow state is not updated properly
}
}).catch(error => {
alert(error)
});
}