1

I am going through reactj hook forms and I find this syntax

<input {...register("firstName")} />

But how to understand this

I generally can understand the below syntax

<input myProp={something} />
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax It can also resolve objects, `register` will probably return it – Sysix Jul 31 '21 at 01:50
  • Does this answer your question? [What does the three dots notation do in Javascript?](https://stackoverflow.com/questions/31048953/what-does-the-three-dots-notation-do-in-javascript) – Nick Parsons Jul 31 '21 at 01:57

1 Answers1

2

The spread syntax is a way to expand an object or an array inline.

I'm assuming you're using react-hook-form (though I could be wrong!) - in that case, the register function returns an object that looks like { onChange, onBlur, name, ref }. Therefore, this

<input {...register("firstName")} />

is the same as

const { onChange, onBlur, name, ref } = register("firstName");
// the above, in case you're also unfamiliar with destructuring, would be the same as
// const registerResult = register("firstName");
// const onChange = registerResult.onChange;
// etc.
<input onChange={onChange} onBlur={onBlur} name={name} ref={ref} />
Brendan Bond
  • 1,737
  • 1
  • 10
  • 8