MCPcopy
hub / github.com/vercel/next.js / Form

Function Form

packages/next/src/client/app-dir/form.tsx:26–145  ·  view source on GitHub ↗
({
  replace,
  scroll,
  prefetch: prefetchProp,
  ref: externalRef,
  ...props
}: FormProps)

Source from the content-addressed store, hash-verified

24export type { FormProps }
25
26export default function Form({
27 replace,
28 scroll,
29 prefetch: prefetchProp,
30 ref: externalRef,
31 ...props
32}: FormProps) {
33 const router = useContext(AppRouterContext)
34
35 const actionProp = props.action
36 const isNavigatingForm = typeof actionProp === 'string'
37
38 // Validate `action`
39 if (process.env.NODE_ENV === 'development') {
40 if (isNavigatingForm) {
41 checkFormActionUrl(actionProp, 'action')
42 }
43 }
44
45 // Validate `prefetch`
46 if (process.env.NODE_ENV === 'development') {
47 if (
48 !(
49 prefetchProp === undefined ||
50 prefetchProp === false ||
51 prefetchProp === null
52 )
53 ) {
54 console.error('The `prefetch` prop of <Form> must be `false` or `null`')
55 }
56
57 if (prefetchProp !== undefined && !isNavigatingForm) {
58 console.error(
59 'Passing `prefetch` to a <Form> whose `action` is a function has no effect.'
60 )
61 }
62 }
63
64 // TODO(runtime-ppr): allow runtime prefetches in Form
65 const prefetch =
66 prefetchProp === false || prefetchProp === null ? prefetchProp : null
67
68 // Validate `scroll` and `replace`
69 if (process.env.NODE_ENV === 'development') {
70 if (!isNavigatingForm && (replace !== undefined || scroll !== undefined)) {
71 console.error(
72 'Passing `replace` or `scroll` to a <Form> whose `action` is a function has no effect.\n' +
73 'See the relevant docs to learn how to control this behavior for navigations triggered from actions:\n' +
74 ' `redirect()` - https://nextjs.org/docs/app/api-reference/functions/redirect#parameters\n' +
75 ' `router.replace()` - https://nextjs.org/docs/app/api-reference/functions/use-router#userouter\n'
76 )
77 }
78 }
79
80 // Clean up any unsupported form props (and warn if present)
81 for (const key of DISALLOWED_FORM_PROPS) {
82 if (key in props) {
83 if (process.env.NODE_ENV === 'development') {

Callers

nothing calls this directly

Calls 7

checkFormActionUrlFunction · 0.90
mountFormInstanceFunction · 0.90
useMergedRefFunction · 0.90
addBasePathFunction · 0.90
onFormSubmitFunction · 0.70
errorMethod · 0.65

Tested by

no test coverage detected