Skip to content
RED SAG Skills
hardStudy question · answer published in JavaScript interview questions, so no pointsreacthooksclosures

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