无法在react-admin中更新SimpleForm TextInput值

问题描述

<SimpleForm
label="resources.xyz"
{...props}

<TextInput
source="latitude"
label="resources.latitude"
defaultValue={
gpsLocation?.capturedLocation?.latitude || props?.record?.latitude
}
value={
gpsLocation?.capturedLocation?.latitude || props?.record?.latitude
}
/>......

在这种情况下,当我们通过单独的函数捕获位置时,gpsLocation?.capturedLocation ?.纬度和经度会发生变化,但是在提交表单时,textInput值就不会变。

解决方法

如果您使用的是props.record组件,则可能未定义

<Edit>。此外,您不应在默认值中使用记录纬度。当记录被加载时,React-admin会负责。最后,react-admin的TextInput是controlled component,因此您不应该自己设置value

这应该有效:

<TextInput
    source="latitude"
    label="resources.latitude"
    defaultValue={gpsLocation?.capturedLocation?.latitude}
/>