0

I'm following the documentation (https://inertiajs.com/forms) of Inertia for custom submit button using the <Form ...> component.

It show as example:

import { useRef } from 'react'
import { Form } from '@inertiajs/react'

export default function CreateUser() {
  const formRef = useRef()

  const handleSubmit = () => {
    formRef.current.submit()
  }

  return (
    <>
      <Form ref={formRef} action="/users" method="post">
        <input type="text" name="name" />
        <button type="submit">Submit</button>
      </Form>

      <button onClick={handleSubmit}>Submit Programmatically</button>
    </>
  )
}

But I need a type for formRef, because the line formRef.current.submit() is warning in TS.

I try with HTMLFormElement but still showing errors.

What are the corrects types here?

I expect to TS don't show errors in my IDE.

I tried const formRef = useRef<HTMLFormElement | null>(null);

Thanks

2
  • Does it not tell you what type it expects? What does the warning say? Are there also errors? What do those say? Commented Aug 18 at 19:39
  • @Thomas Type 'RefObject<HTMLFormElement | null>' is not assignable to type 'Ref<FormComponentSlotProps> | undefined'... but FormComponentSlotProps doesn't exists. So I try to change it but always I got a new error Commented Aug 18 at 19:43

1 Answer 1

1

I found a solution. You need import then add the type.

import { FormComponentRef } from '@inertiajs/core';
const formRef = useRef<FormComponentRef>(null);

Thanks for all

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.