我想做一些响应逻辑,当一个值发生变化时,它会触发表单中的其他值发生变化。
我正在使用 mantine 形式,到目前为止我能想到的最好的方法如下:
const onUserChange = (e) => {
// form.values.acounts.user contains the previous user value
// e contains the incoming update to it
form.setFieldValue('other.property.associated.with.user', e);
}
<Select label="User"
data={users}
{...form.getInputProps(`accounts.user`)}
onChange={(e) => {
onUserChange(e);
form.getInputProps(`accounts.user`).onChange(e)
}}
></Select>
Run Code Online (Sandbox Code Playgroud)
这种方法“似乎”不错,但我不确定是否有更好的方法。有人以前遇到过这个吗?也许一些简洁的回调语法之类的?