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 }
}Discussion 0
Join free to post in the discussion.
Related questions
- Why React lists need keysFrontend Developer
- Input that ignores typingFrontend Developer
- Changing a prop inside a childFrontend Developer
- What JSX turns intoFrontend Developer
- A component written in lowercaseFrontend Developer
- Adding a CSS class in JSXFrontend Developer
- Returning two elementsFrontend Developer
- A stray 0 on the pageFrontend Developer