Professional Documents
Culture Documents
<h2>{title}</h2> <>
</>)}
} <div>
} </div>
);
Inline If-Else
}
return (
</div> ======================
);
}
AXIOS
First, you're going to make a request According to the API, this needs to be
for individual posts. If you look at the performed on the /posts endpoint. If you
endpoint, you are getting the first look at the code below, you'll see that
post from the /posts endpoint: there's a button to create a post:
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
<button onClick={createPost}>Create Post</button>
</div>
);}
In this case, you'll update the first As an example, we'll delete the first
post. post.
To do so, you'll once again create a Note that you do not need a second
button. But this time, the button will argument whatsoever to perform this
call a function to update a post: request:
}
In most cases, you do not need the data
that's returned from the .delete() method.
In the code above, you use the PUT
method from Axios. And like with the
POST method, you include the
properties that you want to be in the
updated resource.
const [ post, setPost] = React.useState(null); To actually consume the value returned when the
promise fulfills, since it is returning a promise, we could
use a .then() block:
error, setError] =
const [
if (error) return `Error: ${error.message}`; You can use await when calling any function that
returns a Promise, including web API functions.
if (! post) return "No post!"
Here is a trivial example:
< div>
hello().then(alert);
h1>{post.title}</h1>
<
p post.body}</p>
< >{
</ div>
);
if (!response.ok) {
throw new Error(`HTTP error! status:
${response.status}`);
}
myFetch()
.catch(e => {
console.log('There has been a problem with your fetch
operation: ' + e.message);
})
=============================
Adding error handling
try {
if (!response.ok) {
image.src = objectURL;
document.body.appendChild(image);
} catch(e) { console.log(e);}}myFetch();