import React, { useState } from 'react'; import './DemoApp.css'; import { dispatch, useEvent } from './use-event'; type AddProductEvent = { type: 'AddProduct'; price: number; }; type RemoveProductEvent = { type: 'RemoveProduct'; price: number; }; type RemoveAllProductEvent = { type: 'RemoveAllProduct'; }; function DemoApp() { return ( <>

react-use-event Demo

npm package version badge {' '}

Product Unit Price Quantity
); } function Product({ name, price }: { name: string; price: number }) { const [quantity, setQuantity] = useState(0); const dispatchAdd = useEvent('AddProduct'); useEvent('RemoveAllProduct', () => { setQuantity(0); }); function add() { setQuantity(quantity + 1); dispatchAdd({ price }); } function remove() { setQuantity(quantity - 1); dispatch('RemoveProduct', { price }); } return ( {' Component'} {name} ${price} {quantity} ); } function Report() { const [totalQuantity, setTotalQuantity] = useState(0); const [totalPrice, setTotalPrice] = useState(0); useEvent('AddProduct', (event) => { setTotalQuantity(totalQuantity + 1); setTotalPrice(totalPrice + event.price); }); useEvent('RemoveProduct', (event) => { setTotalQuantity(totalQuantity - 1); setTotalPrice(totalPrice - event.price); }); useEvent('RemoveAllProduct', () => { setTotalQuantity(0); setTotalPrice(0); }); function removeAll() { dispatch('RemoveAllProduct', {}); } return ( Total Quantity Total Price {' Component'} {totalQuantity.toLocaleString()} ${totalPrice.toLocaleString()} ); } export default DemoApp;