0

我有以下代码:

...
      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 时不会发送相同的值。

在此处输入图像描述

以 +44 开头的值是我想发送给我的 BE 但它仍然发送里面的东西props.input.value

4

2 回答 2

0

傻我,我忘了更新onChange:

<PhoneNumberInput
  disabled={disabled}
  data-bdd={`customer_details_field_${fieldKey}`}
  id={id}
  value={props.input.value}
  onChange={props.input.onChange}
  {...props}
于 2020-01-31T14:29:12.527 回答
0

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

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

它会自动将 props 传递给组件

于 2020-01-31T14:19:23.223 回答