Trigger Function (client) —> Action Function (server) —> Mutation Function (server)
Show more logic, less code details.
Below is a code example of creating an object from clicking a submit button in a form in an object editor works both for creating and updating.
import { mutate } from 'swr';
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
setIsSubmitting(true);
e.preventDefault();
if (isCreating) {
await addCommentAction(formData, userID);
} else {
await updateCommentAction(formData, userID);
}
setIsSubmitting(false);
mutate(['comments', userID]); // for updating the list of articles
closeModal(); // in this example, creating and editing are in a same editor that shows as a modal
};
Show more logic, less code details.