How can i do delete operation Inertia & React without page reloading.
ArticleController:
public function destroy(Article $article) {
$article->delete();
return redirect('/admin');
}
jsx:
function ArticleItem({ article }) {
const { url } = usePage()
const inAdminPage = url.includes('/admin') ? true : false
function onDeleteSubmitEventHandler(e) {
e.preventDefault()
Inertia.delete(`/article/${article.id}`)
}
return (
<>....
{inAdminPage &&
<>
<div onClick={handleOptionsClick} className="hover:bg-gray-100 hover:cursor-pointer transition rounded-full p-2 relative">
<img src="/icon/options.svg" className="w-1" />
</div>
{options &&
<div id="options" className="absolute right-7 bottom-0 shadow-md">
<ul>
<form onSubmit={onDeleteSubmitEventHandler}>
<button type="submit" className="hover:bg-gray-200 hover:cursor-pointer bg-white px-4 py-3" >Delete</button>
</form>
<li className="hover:bg-gray-200 bg-white px-4 py-3">Edit</li>
</ul>
</div>
}
</>
}
</>
)
}
The code above currently requires a page reload to reflect the deletion of data. I want the deletion to occur in real-time without the need for a page reload.
How can i do it?
Thank You
2
Answers
try to use this in your handleSubmit
import { router } from ‘@inertiajs/react’;
function onDeleteSubmitEventHandler(e) {
e.preventDefault()
Inertia.delete(
/article/${article.id}
)router.reload({ only: [‘article’] })
}
https://inertiajs.com/partial-reloads
as y con see in inertia.doc
ps: paste even your controller to be clearer
and paste this :
return to_route(‘yourROUTE’)
try this code
first update your destroy function
then modified your code
indicating the success of the deletion operation.