【问题标题】:React - issue with initialValues ant design when using referenced componentReact - 使用引用组件时initialValues ant设计的问题
【发布时间】:2021-02-09 14:38:34
【问题描述】:

使用 ant 设计输入和表单,当引用组件时,我无法使用 intialValues。在这种情况下,我试图将 CustomInput.js(一个 ant 设计输入)作为 @987654323 传递到我的表单中@。除了初始值之外,一切都正常。

有什么想法可以让intialValues 工作吗?

https://codesandbox.io/s/initialvalues-forked-keftp?file=/index.js:382-414

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    您必须将自定义组件中的道具传递给Input,特别是value 道具:

    function CustomInput(
      { field, value = "", onChange = (e) => {}, disabled, ...rest },
      ref
    ) {
      return (
        <Input
          ref={ref}
          value={value}
          onChange={onChange}
          disabled={disabled}
          {...rest}
        />
      );
    }
    

    DEMO

    注意:我不确定您的 field 属性是什么。由于它不是组件 API 的一部分受支持的道具,因此我将其省略了。

    【讨论】:

      猜你喜欢
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2019-06-01
      • 2020-03-10
      • 2021-04-15
      • 2020-10-06
      • 2021-07-01
      相关资源
      最近更新 更多