reactjs React钩状语域中省略号的意义何在?

wh6knrhe  于 2022-12-22  发布在  React
关注(0)|答案(1)|浏览(121)

在react-hook-form包的文档中,特别是register部分,它显示了以下符号:

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

所以我的问题是这里省略号的目的是什么?为什么我们只做register(...)还不够?
谢啦,谢啦

von4xj4u

von4xj4u1#

register返回多个值和方法,这些值和方法传递给标准html输入
Spread是js语法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
下面两个例子是相同的:

const { onChange, onBlur, name, ref } = register('firstName'); 
// include type check against field path with the name you have supplied.
        
<input 
  onChange={onChange} // assign onChange event 
  onBlur={onBlur} // assign onBlur event
  name={name} // assign name prop
  ref={ref} // assign ref prop
/>
// same as above
<input {...register('firstName')} />

相关问题