You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a small question concerning state update, when we delete a blog record directly from Home in BlogList component, not in BlogDetails.
In Lesson 13 we've used the callback function to handle delete:
import{useState}from"react";importBlogListfrom"./BlogList";constHome=()=>{const[blogs,setBlogs]=useState([{title: 'My new website',body: 'lorem ipsum...',author: 'mario',id: 1},{title: 'Welcome party!',body: 'lorem ipsum...',author: 'yoshi',id: 2},{title: 'Web dev top tips',body: 'lorem ipsum...',author: 'mario',id: 3}])consthandleDelete=(id)=>{constnewBlogs=blogs.filter(blog=>blog.id!==id);setBlogs(newBlogs);}return(<divclassName="home"><BlogListblogs={blogs}title="All Blogs"handleDelete={handleDelete}/></div>);}exportdefaultHome;
constBlogList=({ blogs, title, handleDelete })=>{return(<divclassName="blog-list"><h2>{title}</h2>{blogs.map(blog=>(<divclassName="blog-preview"key={blog.id}><h2>{blog.title}</h2><p>Written by {blog.author}</p><buttononClick={()=>handleDelete(blog.id)}>delete blog</button></div>))}</div>);}exportdefaultBlogList;
Then in Lesson 31 we've moved all logic to useFetch hook, so state is updated for BlogDetails page:
import{useHistory,useParams}from"react-router-dom";importuseFetchfrom"./useFetch";constBlogDetails=()=>{const{ id }=useParams();const{data: blog, error, isPending }=useFetch('http://localhost:8000/blogs/'+id);consthistory=useHistory();consthandleClick=()=>{fetch('http://localhost:8000/blogs/'+blog.id,{method: 'DELETE'}).then(()=>{history.push('/');})}return(<divclassName="blog-details">{isPending&&<div>Loading...</div>}{error&&<div>{error}</div>}{blog&&(<article><h2>{blog.title}</h2><p>Written by {blog.author}</p><div>{blog.body}</div><buttononClick={handleClick}>delete</button></article>)}</div>);}exportdefaultBlogDetails;
And what if I want to add delete button directly to the BlogList component to delete a blog record from Home page?
What is the easiest or the best approach to achieve something like this?
Notice handleDeleteClick is in BlogList not in BlogDetails.
import{Link}from"react-router-dom";constBlogList=({blogs, title})=>{consthandleDeleteClick=(id)=>{fetch(`http://localhost:8000/blogs/${id}`,{method: 'DELETE'});}return(<divclassName="blog-list"><h2>{title}</h2>{blogs.map((blog)=>(<divclassName="blog-preview"key={blog.id}><Linkto={`/blogs/${blog.id}`}><h2>{blog.title}</h2><p>Written by {blog.author}</p><buttononClick={()=>handleDeleteClick(blog.id)}>delete</button></Link></div>))}</div>);}exportdefaultBlogList;
Hi Shaun,
THANKS for incredible videos!
I have a small question concerning state update, when we delete a blog record directly from
Home
inBlogList
component, not inBlogDetails
.In Lesson 13 we've used the callback function to handle delete:
Then in Lesson 31 we've moved all logic to
useFetch
hook, so state is updated forBlogDetails
page:And what if I want to add delete button directly to the
BlogList
component to delete a blog record from Home page?What is the easiest or the best approach to achieve something like this?
Notice
handleDeleteClick
is inBlogList
not inBlogDetails
.Thanks!
The text was updated successfully, but these errors were encountered: