Closed skorphil closed 8 months ago
action
described on 2:00 https://www.youtube.com/watch?v=dDpZfOQBMaU
For this scenario it is needed to use db instead of a temporary json file.
looking for suggestion on simple DB https://www.reddit.com/r/nextjs/comments/1b4qnqg/suggestion_on_simple_database_as_an_alternative/
Mongo with nodejs https://www.mongodb.com/nodejs-database Moongose claims to simplife forking with mongodb
Small databases:
Task intercects with
Maybe to explore firestore if will be deployed on google firebase later
ORM tools help to map oop app to relational db
Mongoose is mapper to MongoDb
Related materials
Use server actions with react-hook-form https://brockherion.dev/blog/posts/using-react-hook-form-with-nextjs-13-server-actions/ https://nehalist.io/react-hook-form-with-nextjs-server-actions/ https://medium.com/@danielmdob/using-next-js-server-actions-with-react-hook-form-4eadbd7f1c67
Nextjs fetching https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
We can invoke server Action via handleSubmit
:
// servrerAction.js
'use server'
export async function getFullName({firstName, lastName}: FullName) {
const loggedContent = `entered name: ${firstName} ${lastName}`;
console.log(loggedContent);
return loggedContent;
}
//form.jsx
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { getFullName } from './api';
const NameForm = () => {
const [serverResponse, setServerResponse] = useState(null);
const { register, handleSubmit } = useForm();
const onSubmit = async (data) => {
const response = await getFullName(data);
setServerResponse(response);
};
return (
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col items-center space-y-2">
<Input placeholder="First Name" {...register('firstName')} />
<Input placeholder="Last Name" {...register('lastName')} />
...
<form>
yes, it's inside useForm()
const { register, handleSubmit } = useForm()
handleSubmit()
returns same as getValues()
- nested object in my case
onClick={formMethods.handleSubmit((data) => console.log(data) )}
can invoke server action
Server action can be invoked via <form action{getFullName}
, like here: https://nehalist.io/react-hook-form-with-nextjs-server-actions/#:~:text=we%20should%20use%20the%20action%20prop
But [here](https://brockherion.dev/blog/posts/using-react-hook-form-with-nextjs-13-server-actions/#:~:text=(%0A%20%20%20%20%3Cform-,onSubmit,-%3D%7Bform) used <form onSubmit={form.handleSubmit((data) => onMyFormSubmit(data))}>
IDK why but since first example reference nextjs docs i will stick to action
Decomposition
What is the most appropriate way of submitting the data?
react-hook-form provides internal mechanisms for that:
They use either
action
oronSubmit
.onSubmit
fires beforeaction
is performed – form-submission-algorithmuseForm({resolver: yupResolver(schema)})
onSubmit
is firedaction
is performed