usestate hook rerender code example

Example 1: useState doesnt rerender component

setNumbers([...old])

Example 2: rerender in hooks testing

import { renderHook, act } from '@testing-library/react-hooks'
import useCounter from './useCounter'

test('should reset counter to updated initial value', () => {
  let initialValue = 0
  const { result, rerender } = renderHook(() => useCounter(initialValue))

  initialValue = 10
  rerender()

  act(() => {
    result.current.reset()
  })

  expect(result.current.count).toBe(10)
})

Tags:

Misc Example