#Should I convert form-related API route -> server action?

3 messages · Page 1 of 1 (latest)

worn dagger
#

Here's my code.

I made a site using pages router, it's super small at the moment so I can afford "big" rewrites at the moment, and wanted to try the app router. Migrating over isn't too bad, I did run into an issue when converting my home page (form sending data to API) to a server component (form using server actions). What stumps me is that I know you can call API routes explicitly server-side, but you can also convert them to server actions which I wanted to attempt, and realised I'm not sure how I'd be able to pass the necessary Request argument.

How do I approach this exactly?

wide sequoiaBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

final kelp
#

Hi 👋

With Server Actions, you can pass your payload as formData by setting values on form fields:

function Page() {

  async function action(formData) {
    "use server"
    const user = formData.get('user')
    const password = formData.get('password')
    // do something with payload
  }

  return (
    <form aciton={action}>
      <input name="user" type="text" />
      <input name="password" type="password" />
      <button>Submit</button>
    </form>
  )
}