Build a useCounter Hook with Limits

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write a custom hook and export it by name: useCounter(options) takes an optional object { initial, min, max } and returns an object { count, increment, decrement, reset }. count starts at initial (0 when it's left out). increment adds one and decrement takes one away, but count never goes above max or below min; without a min or max there's no limit on that side. reset puts count back to initial. The three functions ignore any arguments, so they can go straight to onClick. The default export App is a guest counter for the preview; the checks test useCounter itself.

Your code must pass

  • Starts at initial, 0 by default
  • increment and decrement change the count by one
  • Stays within min and max
  • reset goes back to the initial count