[英]How to listen to onChange of the Field component in React-Final-Form?
Redux-form "Field" component provides onChange property . Redux-form "Field" 组件提供了onChange 属性。 A callback that will be called whenever an onChange event is fired from the underlying input.
每当从底层输入触发 onChange 事件时将调用的回调。 This callback allows to get "newValue" and "previousValue" for the Field.
此回调允许为字段获取“newValue”和“previousValue”。
React-final-form "Field" component doesn't have this property. React-final-form "Field" 组件没有这个属性。
So, how I can get the same functionality?那么,我如何才能获得相同的功能?
React-final-form handles this functionality with a tiny external package. React-final-form 使用一个很小的外部包来处理这个功能。
Basically it is an additional component to add inside the form that binds to the element using its name:基本上它是一个附加组件,添加到使用其名称绑定到元素的表单中:
<Field name="foo" component="input" type="checkbox" />
<OnChange name="foo">
{(value, previous) => {
// do something
}}
</OnChange>
The current documentation can be found here:当前文档可以在这里找到:
https://github.com/final-form/react-final-form-listeners#onchange https://github.com/final-form/react-final-form-listeners#onchange
The idea under change detection is to subscribe to value changes of Field
and call your custom onChange
handler when value actually changes.更改检测下的想法是订阅
Field
值更改并在值实际更改时调用您的自定义onChange
处理程序。 I prepared simplified example where you can see it in action.我准备了简化的示例,您可以在其中看到它的实际效果。 Details are in
MyField.js
file.详细信息在
MyField.js
文件中。
As the result you can use it just as with redux-form
:因此,您可以像使用
redux-form
一样使用它:
<MyField component="input" name="firstName" onChange={(val, prevVal) => console.log(val, prevVal)} />
I haven't used redux-form, but I added a super simple wrapper around the Field component to listen to onChange like this:我没有使用过 redux-form,但是我在 Field 组件周围添加了一个超级简单的包装器来监听 onChange,如下所示:
const Input = props => {
const {
name,
validate,
onChange,
...rest
} = props;
return (
<Field name={name} validate={validate}>
{({input, meta}) => {
return (
<input
{...input}
{...rest}
onChange={(e) => {
input.onChange(e); //final-form's onChange
if (onChange) { //props.onChange
onChange(e);
}
}}
/>
)}}
</Field>
);
};
One could use the Field's parse
attribute and provide a function that does what you need with the value:可以使用 Field 的
parse
属性并提供一个函数来执行您需要的值:
<Field
parse={value => {
// Do what you want with `value`
return value;
}}
// ...
/>
You need to use the ExternalModificationDetector
component to listen for changes on the field component like this:您需要使用
ExternalModificationDetector
组件来侦听字段组件上的更改,如下所示:
<ExternalModificationDetector name="abc">
{externallyModified => (
<BooleanDecay value={externallyModified} delay={1000}>
{highlight => (
<Field
//field properties here
/>
)}
</BooleanDecay>
)}
</ExternalModificationDetector>
By wrapping a stateful
ExternalModificationDetector
component in aField
component, we can listen for changes to a field's value, and by knowing whether or not the field is active, deduce when a field's value changes due to external influences.通过将有状态的
ExternalModificationDetector
组件包装在Field
组件中,我们可以监听字段值的变化,并通过了解该字段是否处于活动状态,推断字段值何时因外部影响而发生变化。Via - React-Final-Form Github Docs
通过 - React-Final-Form Github Docs
Here is a sandbox example provided in the React-Final-Form Docs: https://codesandbox.io/s/3x989zl866这是React-Final-Form文档中提供的沙箱示例: https : //codesandbox.io/s/3x989zl866
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.