简体   繁体   English

React Hook Forms Controller 问题

[英]React Hook Forms Controller issues

I am quiet new to React Hook Forms.我对 React Hook Forms 很陌生。 I am trying integrate Material UI with react hook forms.我正在尝试将 Material UI 与反应挂钩 forms 集成。 I am avoiding the use of normal submit rather using onClick on a button to trigger submit, I am using controller but for some reason when I submit my values always reflect default values and not the actual values on the input.我避免使用普通提交,而是在按钮上使用 onClick 来触发提交,我使用的是 controller 但由于某种原因,当我提交我的值时总是反映默认值而不是输入上的实际值。

 <Controller name={name} control={control} defaultValue={defaultValue} render={() => ( <QControl uiProp={uiProp} context={context} handleChange={handleChange} /> )} />

 <Button type="submit" className="form-actions" variant="contained" color="primary" startIcon={<SaveIcon />} onClick={onSubmission} >

 const { control, handleSubmit } = useForm(); const onSubmit = (data) => console.log(data); const onSubmission = handleSubmit(onSubmit);

I'm looking through the Material UI docs and I can't find the Controller component, just the FormControl component.我正在查看 Material UI 文档,但找不到 Controller 组件,只有FormControl组件。

I'm not familiar with Material UI, but I know about React and it doesn't seem that you have any onChange functions that are updating the values from your default, so the JS values would only be the default when you use the onSubmit .我不熟悉 Material UI,但我了解 React,而且您似乎没有任何onChange函数可以从默认值更新值,因此 JS 值仅在您使用onSubmit时才是默认值。

I figured it out.我想到了。 The default Value prop on the controller then passes it as a value to the material ui component in props of the render prop. controller 上的默认 Value 道具然后将其作为值传递给渲染道具道具中的材质 ui 组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM