如何使用 API 响应更新 React admin 中的 textInput 值?

问题描述 投票:0回答:1

如何更改 React admin 中的 textInput 值以获取从 API 调用收到的响应的更新? 这是代码:

         <TextInput source="..." label="..." />

我尝试从 useForm hook 中使用 setValue,getValues 。 它不起作用。

react-admin textinput
1个回答
0
投票

[电子邮件受保护] 现在使用

react-hook-form
。所以你可以使用
useFormContext
来获取
setValue

但只有当你用

FormProvider
包裹表单时它才会生效(在react-admin中,提供程序将类似于 )


const ControlledTextInput = ({ value, ...props }) => {
  const { setValue } = useFormContext();

  React.useEffect(() => {
    setValue(props.source, value);
  }, [value]);

  return <TextInput {...props} />;
};


// ==========

<SimpleFormConfigurable>
   <ControlledTextInput
      source="title"
      validate={required("Required field")}
      value="this value was provide by API response"
   />
</SimpleFormConfigurable>
© www.soinside.com 2019 - 2024. All rights reserved.