redux-form 中 `normalize` 和 `parse` 回调的区别

Difference between `normalize` and `parse` callbacks in redux-form

current redux-form documentation(撰写本文时版本 6.5.0)提到了 Field 对象的 2 个回调:normalizeparse.

这两个描述听起来很相似:它们获取用户在输入字段中输入的值并将其转换为存储在 redux 中的值。

这两个回调有什么区别?

这两个函数本质上做的是完全相同的事情,即将用户输入的 value 转换为 Field 并在存储到 redux 存储之前对其进行转换。

区别在于flavor of these functions and the order in which they are called

  1. parse 解析字符串输入值应该将其转换为您希望存储在 redux 存储中的类型,例如您将日期字符串从日期选择器解析为 Date 对象
  2. normalize 意味着在 redux 存储中强制输入值的某些格式,for example ensuring that phone numbers are stored in a cohesive format

当谈到在redux-form值生命周期中调用这些方法的顺序时:parsenormalize之前被调用,这意味着normalize被调用使用解析的输入值。

所以简而言之,使用 parse 将用户输入(通常为字符串形式)转换为适合您需要的类型。使用 normalize 强制用户使用特定的输入格式。

这就是 Value Lifecycle Hooks 页面试图解释的内容。