简体   繁体   English

在提交时从 react-hook-form 中的 react-select 重置选定的值?

[英]Reset selected values from react-select within react-hook-form on submit?

I'm in the process of replacing some select inputs in my app to use react-select .我正在替换我的应用程序中的一些select输入以使用react-select Prior to using react-select , I was clearing the selected options in the form on submit using the .reset() function, which does not appear to cause any effect now.在使用react-select之前,我使用.reset() function 在提交时清除表单中的选定选项,现在似乎没有任何影响。

I tried to store a variable in the onSubmit function with null as the value to be set to the defaultValue prop in the Controller thinking it would reset things.我尝试在onSubmit function 中存储一个变量,其中null作为要设置为Controller中的defaultValue属性的值,认为它会重置。 However that didn't work.然而这并没有奏效。 What would be a clean way to handle this?什么是处理这个问题的干净方法?

Parent form component:父表单组件:

function AddActivityForm(props) {

  const { register, handleSubmit, control, watch, errors } = useForm();

  const onSubmit = (data) => {
    //Additional logic here
    document.getElementById("activity-entry-form").reset();
  };

  return (
    <Form id="activity-entry-form" onSubmit={handleSubmit(onSubmit)}>
          <ActivityPredecessorInput
            activities={props.activities}
            formCont={control}
          />
      <Button variant="primary" type="submit" className="mb-2">
        Submit
      </Button>
    </Form>
  );
}

export default AddActivityForm;

Child using Select from react-select :使用来自react-select Select的孩子:

function ActivityPredecessorInput(props) {
  const activities = props.activities;
  const options = activities.map((activity, index) => ({
    key: index,
    value: activity.itemname,
    label: activity.itemname,
  }));
  return (
    <Form.Group controlId="" className="mx-2">
      <Form.Label>Activities Before</Form.Label>
      <Controller
        as={
          <Select
            isMulti
            options={options}
            className="basic-multi-select"
            classNamePrefix="select"
          />
        }
        control={props.formCont}
        name="activitiesbefore"
        defaultValue={""}
      />
    </Form.Group>
  );
}

export default ActivityPredecessorInput;

Answering my own question based on the example provided by @Bill in the comments in the original post.根据@Bill 在原始帖子的评论中提供的示例回答我自己的问题。 React-hook-form provides a reset method that takes care of the situation pretty simply. React-hook-form 提供了一种重置方法,可以非常简单地处理这种情况。

Create a const to store default values (stored in the parent component in this case).创建一个const来存储默认值(在这种情况下存储在父组件中)。

const defaultValues = {
  activitiesbefore: "",
};

Include reset method in the useForm const .useForm const中包含reset方法。

const { register, handleSubmit, reset, control, watch, errors } = useForm();

Call the reset method in the onSubmit function passing defaultValues .在传递defaultValuesonSubmit function 中调用reset方法。

reset(defaultValues);

You can pass the onClick onClick={()=> setValue("activitiesbefore", "")} parameter with the setValue value from useForm const { register, handleSubmit, errors, reset, control, setValue} = useForm();您可以将 onClick onClick={()=> setValue("activitiesbefore", "")}参数与来自 useForm const { register, handleSubmit, errors, reset, control, setValue} = useForm(); to the reset button, or to the submit button到重置按钮,或提交按钮

暂无
暂无

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

相关问题 如何将 React-Select 中的选定值转发到 react-hook-form - How to forwardRef selected value from React-Select to react-hook-form 如何在react-hook-form中的react-select中使用useEffect中的条件defaultValues? - How to have conditional defaultValues from useEffect in react-select within react-hook-form? 从 react-hook-form 中的 react-select 组件获取值? - Getting values from react-select component in react-hook-form? 如何仅使用 react-select 和 react-hook-form 发送所选选项的值 - How to only send the value of selected option with react-select and react-hook-form React-hook-form 和 react-select AsyncSelect:未显示来自选择的标签 - React-hook-form and react-select AsyncSelect: label from select not being shown 当 React-Select 改变时,React-hook-form 不会改变表单的有效性 - React-hook-form doesn't change the form validity when React-Select is changed 为什么 select 没有以 react-hook-form 列出 reset() 上的加载值? - Why aren't select lists loading values on reset() in react-hook-form? 功能组件中的 react-hook-form react-select 给出警告 - react-hook-form react-select in functional component gives warning 使用 react-select 和 react-hook-form 返回正确的值 - Returning correct value using react-select and react-hook-form 带有 react-select formState 的 React-hook-form 无效但 controller 没有给出错误 - React-hook-form with react-select formState not valid but controller don't giving an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM