logo

Summary of React Version 19

Major New Features

  1. Actions
  • Enhanced support for asynchronous functions: By using useTransition, you can automatically manage pending states, error handling, and optimistic updates in asynchronous functions.
const [isPending, startTransition] = useTransition()

const handleSubmit = () => {
  startTransition(async () => {
    const error = await updateName(name)
    if (error) {
      setError(error)
      return
    }
    redirect('/path')
  })
}
  1. New Hook: useActionState
  • Easily handle common cases of Actions: Manage the results of asynchronous actions, pending states, errors, and more.
const [error, submitAction, isPending] = useActionState(
  async (prevState, formData) => {
    const error = await updateName(formData.get('name'))
    if (error) return error
    redirect('/path')
    return null
  },
  null
)
  1. React DOM:
    Actions and useFormStatus
  • Pass functions as action and formAction props: Automatically manage form submissions and reset the form after submission.

3 minutes to read

Prime number check

function isPrime(num) {
  if (num === 2) {
    return true
  } else if (num % 2 === 0) {
    return false
  }
  let sqrt = parseInt(Math.sqrt(num))
  for (let i = 3; i <= sqrt; i = i + 2) {
    if (num % i === 0) {
      return false
    }
  }
  return true
}
One minute to read

Finding Divisors

Divisors: An integer that can divide another integer without a remainder.

  1. Finding divisors by dividing all numbers

Condition: Add the number if the remainder is 0 when divided.

Skip if the condition is not met.

Range of condition: The divisor can be at most the number itself, so set the loop to iterate up to the number.

    let num = 8; // 약수를 찾기 위한 정수 설정
    let result = []
    let index = 1;
    
    while (index <= num) {
      if (num % index === 0) result.push(index)
      index++
    }
    console.log(result) // [ 1, 2, 4, 8 ]
  1. Checking only up to half of the given number

Divisors, except for the number itself, cannot be larger than num / 2, so check only up to half.

2 minutes to read