使用 MUI 的声明式 Formik 表单
我想将 formik 与 MUI 一起使用。似乎没有明确的途径来做到这一点,文档没有意义,并且与文档相比,示例也毫无意义。
例如。 此页面位于 formik 示例的顶层。这是我在 formik 网站上找到的唯一集成 MUI 的示例。
- 该示例使用 hooks 方法。似乎没有组件方法的示例
- 示例从核心材质UI包中导入按钮和文本字段。但是,在此沙箱中,组件是从 @mui/material 导入的,并形成 formik-mui。为什么有两个不同的包?为什么文档中没有涵盖这一点?
我想做的就是构建一个带有 mui 样式的 formik 表单,但我找不到任何相关文档。
例如,如果我想使用 MUI 文本字段,有在线示例建议使用此方法。但是,我再次在文档中找不到此内容,并且没有解释此处发生的情况或如何将任何道具应用于此文本字段。
<Form>
<Field
type="email"
name="email"
component={TextField}
color={"error"}
/>
我在这里缺少什么?我只想使用 MUI 构建声明式 formik 表单。
I would like to use formik with MUI. There doesn't seem to be a clear path to do this, the documentation does not make sense and the examples are nonsensical with compared to the docs.
For example. This page is at the top level of formik's examples. It is the only example that I can find on formik's site where MUI is integrated.
- The example uses the hooks method. There doesn't seem to be an example of the component method
- The example imports the button and the text field from the core material UI package. However, in this sandbox, components are imported from @mui/material and form formik-mui. Why two different package? Why isn't this covered in the docs?
All I want to do is build a formik form with mui styling and I can't find any docs for that.
For example, if I want to use a MUI text field, there are online examples that suggest using this method. However, once again I cannot find this in the docs and there is no explanation as to what is happening here or how I apply any props to this text field.
<Form>
<Field
type="email"
name="email"
component={TextField}
color={"error"}
/>
What am I missing here? I just want to build declarative formik forms with MUI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MuI 和 Formik 都有详细的文档记录,唯一的问题是两个文档中都缺少连接部分。
对于声明式 formik,您应该从 hook 切换到 render prop。
澄清一下您对连接部分的困惑。
Mui 不使用 Formik,因此您必须自己编写所有表单字段以连接到 Formik。
对于那些重复性的工作,有这些整洁的库 formik-mui。
如果您使用该库,则只剩下以下模板。
MuI and Formik are well documented, the only issue is the connection part is missing in both documentations.
For declarative formik you should switch from hook to render prop.
To clear a little bit your confusion with the connection part.
Mui don't use Formik so you must code an all form fields the connection to formik yourself.
For those repetative work it exists these neat library formik-mui.
If you use the library, it's just the following template left.