我有以下代码:
...
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
