代码之家  ›  专栏  ›  技术社区  ›  Aessandro

React final form不接受自定义输入值

  •  0
  • Aessandro  · 技术社区  · 6 年前

    我有以下代码:

    ...
          return (
            <FormItem key={name}>
              <Label htmlFor={id}>{camelCaseToTitleCase(fieldKey)}</Label>
              {
                fieldKey === 'homePhone' 
                ?
                  <Field name={`${key}.${fieldKey}.value`} validate={validate(fieldKey)}>
                    {props => 
                      (
                        <>
                        <PhoneNumberInput
                          disabled={disabled}
                          data-bdd={`customer_details_field_${fieldKey}`}
                          id={id}
                          value={props.input.value}
                          // onChange={(val: string):any => console.log(val)}
                          {...props}
                        />
                        </>
                      )
                    }
                  </Field>
    ...
    

    其中phoneinput是:

    const PhoneNumberInput: React.FC<PhoneNumberInputProps> = (props) => {
      const {
        disabled,
        id,
        label,
        value
      } = props
    
      const [updatedValue, setUpdatedValue] = useState(value)
      const DEFAULT_COUNTRY_VALUE = 'GB'
    
      const handleONChange = (val: string) => {
        setUpdatedValue(val)
      }
    
      return (
        <>
          <Label htmlFor={id}>{label}</Label>
          <PhoneInput
            disabled={disabled}
            id={id}
            defaultCountry={DEFAULT_COUNTRY_VALUE}
            value={updatedValue}
            onChange={(phone: string) => handleONChange(phone)}
          />
          value: {updatedValue}
        </>
      )
    }
    

    当我提交表单时 PhoneNumberInput 仍然来自 props.input.value 不反映更新内容 value: {updatedValue} .

    phoneInput组件确实显示了更新后的值,但在提交from时不会发送相同的值。

    enter image description here

    从+44开始的值是我想发送给我的bee的值,但它仍然发送曾经在里面的内容 props.input.value属性

    2 回复  |  直到 6 年前
        1
  •  0
  •   Lucas Claude    6 年前

    使phoneInput组件成为字段的组件,如下所示:

    <Field
      name={`${key}.${fieldKey}.value`}
      validate={validate(fieldKey)}
      component={phoneInput}
      disabled={disabled}
      data-bdd={`customer_details_field_${fieldKey}`}
      id={id}
    />
    

    它会自动将道具传递给组件

        2
  •  0
  •   Aessandro    6 年前

    我真傻,我忘了更新一下:

    <PhoneNumberInput
      disabled={disabled}
      data-bdd={`customer_details_field_${fieldKey}`}
      id={id}
      value={props.input.value}
      onChange={props.input.onChange}
      {...props}
    
    推荐文章