Comment

Comment by vcanales

I know it's not the point, but you can generalize the input handler in the first example by using the name of the input field as a key — in most simple forms, at least:

```

const Form = () => {

  const [formData, setFormData] = useState({ firstName: '', lastName: '' });

  function handleChange(event) {
    const { target } = event;
    const { name, value } = target; // Destructuring in steps to make it easier to figure out where values come from.

    setFormData({
      ...formData,
      [name]: value
    });
  }
  

  return (
    <>
      <input name="firstName" value={formData.firstName} onChange={handleChange} />
      <input name="lastName" value={formData.lastName} onChange={handleChange} />
    </>
  )
}

```

parent vcanales 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton