【问题标题】:How to add a reference input not in a form with react-admin如何使用 react-admin 添加不在表单中的参考输入
【发布时间】:2021-06-09 14:52:23
【问题描述】:

我正在使用 react-admin 并获得解决该问题的堆栈:

我应该如何创建一个带有引用输入的组件,而不是在表单内?

当我尝试添加它时出现错误:Error: useField must be used inside of a <Form> component

我尝试将其插入表单,但因为我没有道具。

我该如何相处?

编辑:

例如:

      <ReferenceInput label="Post" source="post_id" reference="posts">
        <AutocompleteInput optionText="title" />
      </ReferenceInput>
      <DateInput source="" />

我想用选择的 post_id 和日期调用函数 generate。

但正如我所说,我得到了错误:

【问题讨论】:

  • 将您的代码添加到问题中,以便更清楚您想要做什么。你想让组件在没有表单的情况下工作吗?
  • 是的,这正是我所需要的。我添加了一些代码来解释它。谢谢!
  • 据我了解,没有表单就无法做到这一点,所有 Input 组件都无法在没有表单的情况下工作。

标签: react-admin


【解决方案1】:

在后台,这里使用了 react-final-form 库: https://final-form.org/docs/react-final-form/getting-started

也许这个 hack 可以帮助您获得表单的链接:

const FormCreate = (props) => {
  const formRef = useRef(null)
  ...
  formRef.current.change(YOU_FIELD_NAME, new_value) // Refer to the form via the link
  ...
  
  return (
    <Create {...rest} >
      <SimpleForm      
        <FormDataConsumer>
          { ({ formData, ...rest }) => {
            formRef.current = useForm() // Save the link to the form 
          }}
        </FormDataConsumer>      
        ...        
        
      </SimpleForm>
    </Create>
  )
}

【讨论】:

    猜你喜欢
    • 2023-02-14
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多