function AddForm() { const [sum, setSum] = useState(0); const [num, setNum] = useState(0); function handleChange(e) { setNum(e.target.value); } function handleSubmit(e) { setSum(sum + Number(num)); e.preventDefault(); } return <form onSubmit={handleSubmit}> <input type="number" value={num} onChange={handleChange} /> <input type="submit" value="Add" /> <p> Sum is {sum} </p> </form>; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter