Grokking React Advanced
Vote

0% completed

​

Extracting Custom Hooks and Designing Their API

Two product rows call the same hook below, and the user clicks the plus button on the first row twice:

import { useState } from 'react'; function useQuantity({ min = 1, max = 10 } = {}) { const [quantity, setQuantity] = useState(min); const increment = () => setQuantity((q) => Math.min(q + 1, max)); const decrement = () => setQuantity((q) => Math.max(q - 1, min)); return { quantity, increment, decrement }; } function ProductRow({ product }) { const { quantity, increment, decrement } = useQuantity({ max: product.stock }); return ( <li> {product

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content