react force rerender hooks code example

Example 1: force update react hooks

const forceUpdate = React.useReducer(() => ({}), {})[1] as () => void

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