【问题标题】:Function components cannot be given refs. Attempts to access this ref will fail in select component函数组件不能被赋予 refs。尝试访问此引用将在选择组件中失败
【发布时间】:2021-08-03 09:11:06
【问题描述】:

在这里我定义了一个选择组件,如果条件为真,我想显示它。 This select field appears when one of the values ​​of the previous select input is selected.但是这里是当新的选择字段(即我的选择组件),我从这个选择中选择一个值,这个值不是由我的表单提交的,并且在我提交表单后执行控制台日志时不存在于我的数据中,但值字段的名称在那里,但不是值。我的控制台中有一条警告说明: 警告:函数组件不能被赋予 refs。尝试访问此 ref 将失败。你的意思是使用 React.forwardRef() 吗?

查看Controller的渲染方法

我的选择组件

export const SelectCompanyField = () => {
    // const [page, setPage] = useState(1);
    // const [perPage, setPerPage] = useState(10);
    const { data, error } = useSWR<ServerResponse, any>(companyUrls.base, url => getDataByParams(companyUrls.base));
    console.log("Data", data, "Error", error);
    console.log(data?.entities);


    return (

        <Select

            showSearch
            placeholder="Choisir une compagnie"
            optionFilterProp="children"
            filterOption={(input, option: any) =>
                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
            }
        >
            {data?.entities.map(d => (
                <option value={d.index} key={d.id}  >
                    {d.name}
                </option>
            ))}

        </Select>


    );
};

条件为真时显示的选择组件

<Col md={24} lg={24} sm={24}>


                    {firstOptionValue &&
                        <div className="ant-form-item">
                            <label className="label">Compagnie <span className="text-danger">*</span></label>

                            <Controller

                                as={<SelectCompanyField />}
                                name="company"
                                control={control}
                                defaultValue={""}
                                rules={{ required: false }}

                            {errors.company && "Company is required"}
                        </div>

                    }
                </Col>

我的数据提交的控制台日志

{
    "firstName": "Atom",
    "lastName": "Proton",
    "username": "xyz@test.ml",
    "password": "00789",
    "phoneNumber": "44258569",
    "profile": "ADMIN",
    "userType": "B2B_CLIENT",
    "company": ""
}

带有空引号的公司部分是它应该具有我在选择组件中选择的字段值的部分。 我只想将字段或选项的选定值显示在我的数据中,以便我可以提交我的表单。 谢谢

【问题讨论】:

标签: reactjs ref react-hook-form


【解决方案1】:

SelectCompanyField 必须是:

export const SelectCompanyField = React.forwardRef(() => {
 ...
});

当使用Controlleras 属性进行渲染时,它会将ref 属性传递给as 属性。 React 看到 ref 被传递,但你没有使用 forwardRef 组件。

除此之外,您还需要实际使用 SelectCompanyField 提供的 Controller 提供的 ref(和 props)

docs will help you out

☝️ 请阅读文档,但您的 SelectCompanyField 会收到这样的道具:

export const SelectCompanyField = React.forwardRef(({field,meta}) => {
   const { onChange, onBlur, value, ref } = field

   return <Select onChange={onChange} value={value} .../>
});

onChange 处理程序和value 添加到您正在呈现的Select 组件是您的工作。我不知道Select 是什么组件——它是一个组件框架吗?它只是一个普通的select 吗? - 所以我不能告诉你怎么做,但是钩子表单文档很清楚。

【讨论】:

  • 嗨@Adam,根据您的建议,警告已消失,但未提交所选输入的值。
  • @MohamedSacko - 查看我的更新,在文档中很清楚 - SelectCompanyField 从表单中接收 onChange 处理程序和 value,您的工作是将它们插入到您的组件中.
  • 我的Selectimport { Button, Col, Input, Row, Select } from "antd";
  • @MohamedSacko - 然后查看the docs for antd 了解如何使用该组件。看起来您只需要将 onChangevalue 道具直接传递给 Select (请参阅我的编辑)。但是我已经尽我所能,是时候开始阅读一些文档而不是我的答案了。如果你尝试了一些东西但它不起作用并且你不确定为什么,那么请回来寻求帮助。
猜你喜欢
  • 2023-03-24
  • 2021-08-24
  • 2020-05-11
  • 1970-01-01
  • 2021-12-01
  • 2020-08-10
  • 2022-08-22
  • 2019-10-22
相关资源
最近更新 更多