Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test: fix next-form tests when running on react 18 #70704

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
'use client'
import * as React from 'react'
import { useActionState, useState } from 'react'
import Form from 'next/form'
import { useRouter } from 'next/router'

export default function Page() {
const isReact18 = typeof React.useActionState !== 'function'

export default isReact18 ? DummyPage : Page

function DummyPage() {
return <>This test cannot run in React 18</>
}

function Page() {
const destination = '/pages-dir/redirected-from-action'
const router = useRouter()
const [, dispatch] = useActionState(() => {
const [, dispatch] = React.useActionState(() => {
const to = destination + '?' + new URLSearchParams({ query })
router.push(to)
}, undefined)

const [query, setQuery] = useState('')
const [query, setQuery] = React.useState('')
return (
<Form action={dispatch} id="search-form">
<input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
'use client'
import * as React from 'react'
import { type ComponentProps, useActionState, useState } from 'react'
import Form from 'next/form'
import { useRouter } from 'next/router'

export default function Page() {
const isReact18 = typeof React.useActionState !== 'function'

export default isReact18 ? DummyPage : Page

function DummyPage() {
return <>This test cannot run in React 18</>
}

function Page() {
const destination = '/pages-dir/redirected-from-action'
const [query, setQuery] = useState('')
const [query, setQuery] = React.useState('')
return (
<Form action="/pages-dir/search" id="search-form">
<input
Expand All @@ -25,9 +32,9 @@ export default function Page() {
function NavigateButton({
to,
...props
}: { to: string } & ComponentProps<'button'>) {
}: { to: string } & React.ComponentProps<'button'>) {
const router = useRouter()
const [, dispatch] = useActionState(() => {
const [, dispatch] = React.useActionState(() => {
router.push(to)
}, undefined)
return <button type="submit" formAction={dispatch} {...props} />
Expand Down
Loading