reactjs - 使用 react-select 和 react-hook-form 返回正确的值

标签 reactjs react-select react-hook-form

我在 AsyncSelect 周围使用 react-hook-forms Controller api,从 react-select 加载选项,因为用户从外部 API 键入。一切正常,除了返回值作为字符串返回 "[object Object]"而不是对象的 fullName 属性。
我的组件:

           <Controller
            control={control}
            name="businessCategory"
            as={
              <AsyncSelect
                className="react-select-container"
                loadOptions={v => handleAutocompleteLookup(v)}
                onChange={handleCategoryInputChange}
                getOptionLabel={option => option.name}
                getOptionValue={option => option.fullName}
              />
            }
          />
我的 handleChange 函数。 SetValue 来自 react-hook-form:
  const handleCategoryInputChange = newValue => {
    return setValue('businessCategory', newValue, true);
  };
我的任何数据都是具有以下形状的对象数组:
{
  fullName: "DJ service"
  id: "gcid:dj"
  name: "DJ service"
  publisher: "GMB"
}
任何有关这方面的线索将不胜感激,谢谢!

最佳答案

按以下方式更新您的代码
在您的导入

import { useForm, Controller } from 'react-hook-form';
import Select from 'react-select';
在你的钩子(Hook)组件中
function Yourcomponent(props){

    const methods = useForm();
    const { handleSubmit } = methods;
    

    const options = [
     { value: '1', label: 'Apple'},
     { value: '2', label: 'Ball'},
     { value: '3', label: 'Cat'},
    ];
    
    const default_value = 1; // you can replace with your default value
    

    // other codes of the component 
    

    function submitHandler(formData){
    
    // values are available in formData

    }


    return(
      <div>
        
        {* other part of your component *}
        <form onSubmit={handleSubmit(submitHandler)} >

           {* other part of your form *}
            <Controller
                control={methods.control}
                defaultValue={default_value}
                name="field_name_product"
                render={({ onChange, value, name, ref }) => (
                    <Select
                        inputRef={ref}
                        classNamePrefix="addl-class"
                        options={options}
                        value={options.find(c => c.value === value)}
                        onChange={val => onChange(val.value)}
                    />
                )}
            />

           {* other part of your form *}
        </form>

        {* other part of your component *}
      </div>
    )
}

关于reactjs - 使用 react-select 和 react-hook-form 返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62795886/

相关文章:

javascript - 第一次渲染时数据返回为未定义

javascript - React 状态反复恢复到旧值

reactjs - React-hook-form 即使在未触及的字段上也会将 `undefined` 更改为空字符串

reactjs - 如何解决 TypeError : path. split is not a function

reactjs - Redux:如何将 ID 传递给 mapStateToProps 函数以从状态中获取具有该 ID 的实体

html - 如何在 react 中将此下拉菜单组件对齐在我的导航栏上特定位置的下方?

reactjs - 使用react-select作为弹出自定义单元格编辑器时,ag-grid中的键盘事件出现问题

reactjs - react-select 将下拉指示器图标更改为 font-awesome 图标不起作用

reactjs - 有条件地禁用 React 中的选择选项

javascript - 在 React Hook 表单中显示来自 Controller 的错误