【问题标题】:React-bootstrap switch not displayingReact-bootstrap 开关不显示
【发布时间】:2020-02-03 06:43:47
【问题描述】:

我正在尝试合并 Formik 和 React-bootstrap。

除了开关组件外,一切正常。

这是我渲染开关的函数:

function ReactSwitch(props){
    return(
        <>
            <Col md={props.col}>
                <Form.Group>
                    <Form.Check 
                        type="switch"
                        id={props.id}
                        name={props.name}
                        label={props.label}
                        checked={props.checked}
                        onChange={props.onChange}
                    />
                </Form.Group>
            </Col>
        </>
    );
}

这是我对 Formik 的初始化:

const formik = useFormik({
        initialValues: {
          wflow_switch: false
        },
        onSubmit: values => {
          alert(JSON.stringify(values, null, 2));
        },
});

请注意,当我将类型从开关更改为复选框时,它会显示一个复选框,但仍然没有标签。我究竟做错了什么?我还在学习 React,所以感谢任何 cmets。

【问题讨论】:

    标签: reactjs react-bootstrap formik


    【解决方案1】:

    我猜你需要使用state 并启用enablereinitialize

    试试这个:

    export default function FormSwitch() {
      // add checked state
      const [checked, setChecked] = useState(false)
    
      const { handleSubmit, values } = useFormik({
        initialValues: {
          // initial value is set 'false' by state 
          switch: checked
        },
        // Control whether Formik should reset the form if initialValues changes
        enableReinitialize: true,
        onSubmit: (values, { setSubmitting }) => {
          setTimeout(() => {
            alert(JSON.stringify(values, null, 2))
            setSubmitting(false)
          }, 400)
        }
      })
      return (
        <form className="form" onSubmit={handleSubmit}>
          <ReactSwitch
            name="switch"
            label="Switch"
            id="switch"
            checked={checked}
            onChange={() => setChecked(!checked)}
          />
          <button type="submit">
            Submit
          </button>
        </form>
      )
    }
    

    编辑:使用useField&lt;Formik&gt; 的不同方法

    import { Formik, useField } from "formik"
    import { Col, Form } from "react-bootstrap"
    
    const ReactSwitch = ({ ...props }) => {
      const [field, form] = useField(props)
    
      return (
        <Col md={props.col}>
          <Form.Group>
            <Form.Check
              type="switch"
              id={props.id}
              checked={field.value.checked}
              onChange={() => form.setFieldValue(props.name, !field.value)}
              {...field}
              {...props}
            />
          </Form.Group>
        </Col>
      )
    }
    
    export default function Switch() {
      return (
        <Formik
          initialValues={{
            switch: false
          }}
          onSubmit={values => alert(JSON.stringify(values, null, 2))}
        >
          {formik => (
            <form onSubmit={formik.handleSubmit}>
              <ReactSwitch label="Switch" name="switch" id="switch" />
              <button type="submit">submit</button>
            </form>
          )}
        </Formik>
      )
    }
    
    

    【讨论】:

    • 我明白了。但这不会破坏Formik控制所有变化的目的吗?抱歉,我是 React 新手。
    • @Adrian 谢谢。我认为不是,因为我们在这里使用useFormikHook,它实际上用于创建&lt;Formik&gt; 的修改版本。但是,我更新了 codeSandbox 并使用了不同的方法 useField&lt;Formik&gt; 请检查一下。
    • 知道了!谢谢@awran5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2023-01-21
    • 2018-06-30
    • 1970-01-01
    • 2019-11-02
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多