React Js / APIs / update data using fetch()
Update Data
-
1. Step
1. define function
const handleUpdate = async () => { const res = await fetch('https://jsonplaceholder.typicode.com/users/1', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 1, name: 'Updated Name', email: 'updated@example.com' }) }); const data = await res.json(); console.log('Updated:', data); }; 1. method
2. headers
3. body
2. example
function App() { const handleUpdate = async () => { const res = await fetch('https://jsonplaceholder.typicode.com/users/1', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 1, name: 'Updated Name', email: 'updated@example.com' }) }); const data = await res.json(); console.log('Updated:', data); }; return ( ); } export default App;
MANVIA BLOG