Skip to content
RED SAGSkills
hard20 pts · first answer countsreacthooksclosures

A custom hook with a stale callback

useCart returns addItem, but addItem always adds to the cart as it was on the first render. What is the bug?

function useCart() {
  const [items, setItems] = useState([])
  const addItem = useCallback((it) => {
    setItems([...items, it])
  }, [])
  return { items, addItem }
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions