【发布时间】:2021-10-14 12:00:42
【问题描述】:
我正在使用react hook表单,遇到了这个使用Controller for material ui TextField的例子
({ field: { ref, ...field } }) => (
<TextField
{...field}
inputRef={ref}
fullWidth
required
label="Current Password"
type="password"
margin="dense"
/>
)
下面是什么意思
({ field: { ref, ...field } })
还有{...field}(扩展为什么)
<TextField
{...field}
inputRef={ref}
fullWidth
required
label="Current Password"
type="password"
margin="dense"
/>
总码是
<Controller
name="old_password"
control={control}
render={({ field: { ref, ...field } }) => (
<TextField
{...field}
inputRef={ref}
fullWidth
required
label="Current Password"
type="password"
margin="dense"
/>
)}
/>
【问题讨论】:
-
它将一个对象解构为一个名为
ref的变量,并将obj.field.ref作为一个值赋值,并将obj.field对象中的所有其余部分减去ref键和将其分配给变量field。{...field}JSX 语法只是意味着它通过 props 将对象传播到组件上 -
obj.field minus the ref key: 没拿到这部分 -
好吧,对象中的所有内容都减去了
ref键...所以如果你有这样的对象:{ ref: 1, two: 2, three: 3 }并且做了const { ref, ...field } = obj,那么你的ref就是1和@ 987654340@ 是{ two: 2, three: 3 } -
所以在
const { ref, ...field } = obj-field只是一个表示或变量名。我更喜欢看到 obj,即const { ref, ...obj } = obj
标签: reactjs ecmascript-6 react-hook-form