【问题标题】:How to pass Material UI data to Formik?如何将 Material UI 数据传递给 Formik?
【发布时间】:2020-04-14 12:32:23
【问题描述】:

我正在尝试将 Material UI 'Select' 添加到我的 Formik 组件,但无法将值传递给 Formik 的 initialValues。

const [hours, setHours] = React.useState('');
const handleHourChange = ({ target }) => {
    setHours(target.value);
};

<Formik
   initialValues={{
     price: ''   //not Material UI. Works.
     hours: hours //from Material UI
   }} 

  <Form>
     <label htmlFor={'price'}> Price </label>
     <Field
        name={'price'}
        type="text"            //this Field (not Material UI) works fine.
     />

     //...

     //the below, which is Material UI's 
     //doesn't send its values to Formik's initialValues

     <FormControl className={classes.formControl}>
        <InputLabel id="demo-simple-select-label">Hours</InputLabel>
        <Select
          name={'hours'} //I added this name prop but not sure it's making any difference
          labelId="demo-simple-select-label"
          id="demo-simple-select"
          value={hours}
          onChange={handleHourChange}>
          <MenuItem value={60}>01</MenuItem>
          <MenuItem value={120}>02</MenuItem>
        </Select>
      </FormControl>

   </Form>
</Formik>

可以做些什么来解决这个问题?我似乎不知道如何正确获取 Material UI 的值以将它们添加到 Formik。

【问题讨论】:

    标签: javascript reactjs material-ui formik


    【解决方案1】:

    对于材质 ui,你可以使用这个很棒的库 https://github.com/stackworx/formik-material-ui

    这是他们的示例代码框 https://codesandbox.io/s/915qlr56rp?file=/src/index.tsx

    如果你不想使用他们的库,你可以使用 Formik 的 component 属性来使用你的自定义组件


    根据您的评论请求,我已在代码沙箱中编写了代码 https://codesandbox.io/s/react-formik-material-ui-select-huzv7?file=/src/App.js

    我不确定为什么你有 formik 值的状态变量。这些由formik处理。我们不需要手动处理它们。

    import React from "react";
    import { Formik, Form, Field } from "formik";
    import {
      Select,
      InputLabel,
      MenuItem,
      FormControl,
      Button
    } from "@material-ui/core";
    import "./styles.css";
    
    const CustomizedSelectForFormik = ({ children, form, field }) => {
      const { name, value } = field;
      const { setFieldValue } = form;
    
      return (
        <Select
          name={name}
          value={value}
          onChange={e => {
            setFieldValue(name, e.target.value);
          }}
        >
          {children}
        </Select>
      );
    };
    
    export default function App() {
      /*
        You don't need to handle the formik values as state.
        Formik handles it itself
        const [hours, setHours] = React.useState("");
        const handleHourChange = ({ target }) => {
          setHours(target.value);
        };
      */
    
      return (
        <Formik
          initialValues={{
            price: "abcv", //not Material UI. Works.
            hours: 60 //from Material UI
          }}
          onSubmit={(values, actions) => {
            alert("values:" + JSON.stringify(values));
          }}
        >
          <Form>
            <label htmlFor={"price"}> Price </label>
            <Field
              name={"price"}
              type="text" //this Field (not Material UI) works fine.
            />
    
            <FormControl>
              <InputLabel id="demo-simple-select-label">Hours</InputLabel>
              <Field name="hours" component={CustomizedSelectForFormik}>
                <MenuItem value={60}>01</MenuItem>
                <MenuItem value={120}>02</MenuItem>
              </Field>
            </FormControl>
            <Button type="submit">Submit</Button>
          </Form>
        </Formik>
      );
    }
    
    

    【讨论】:

    • 我一定会去看看这个图书馆!谢谢。您能详细说明一下“Formik 的组件道具”吗?
    • 谢谢!你的回答对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2020-03-19
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多