Skip to content

Instantly share code, notes, and snippets.

@lovemycodesnippets
Created April 22, 2024 18:59
Show Gist options
  • Save lovemycodesnippets/ea80ab39f4dd8b261391a91ecb561755 to your computer and use it in GitHub Desktop.
Save lovemycodesnippets/ea80ab39f4dd8b261391a91ecb561755 to your computer and use it in GitHub Desktop.
AbortController API in React_2
import axios from 'axios';
import { useState } from 'react';
function App() {
const [pictureUrl, setPictureUrl] = useState('');
const getPicture = (e) => {
e.preventDefault();
axios
.get('https://randomuser.me/api/')
.then((res) => {
setPictureUrl(res.data.results[0].picture.large);
})
.catch((err) => {
console.log(err);
});
};
return (
<div
style={{
display: 'flex',
alignItems: 'center',
gap: '2rem',
flexDirection: 'column',
marginTop: '2rem',
}}
>
<div style={{ display: 'flex', gap: '2rem' }}>
<button onClick={getPicture}>Get Random Picture</button>
<button> Cancel Request</button>
</div>
<div>
{pictureUrl !== '' ? <img src={pictureUrl} alt='random user' /> : null}
</div>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment