使用 redux-form 在多个不同的挂载点绑定表单
Binding forms at multiple different mount points with redux-form
正如我从 react-form 文档中了解到的那样,您通常会在 reducer 树的根部放置一个 form
reducer,而 redux-form 会将每个表单的状态放入此 form
键中, 基于表单的名称。
然而这会导致一个奇怪的结构,其中某棵树的表单数据在表单键下,而不是与它相关的其他状态数据。在此示例中,应用程序存储来自服务器的可能帐户类型列表,逻辑上应将其与注册表单本身分组。然而,使用 redux-form 它们将位于完全不同的子树中:
signup
/
forms
/ \
/ login
/
root
\ login--signinMethods
\ /
header
\
signup--accountTypes
我更喜欢像这样更有条理的树:
form
/
signup
/ \
/ accountTypes
/
root
\ signinMethods
\ /
login
\
form
redux-form 或任何其他 redux 库是否可行?
尝试使用 reduxMountPoint 选项:
The use of this property is highly discouraged, but if you absolutely
need to mount your redux-form reducer at somewhere other than form in
your Redux state, you will need to specify the key you mounted it
under with this property. Defaults to 'form'.
它的构建方式是这样的(一个 reducer 处理所有表单),不需要在每次需要新表单时都添加一个新的 reducer。您只需添加一次减速器,大部分时间都可以忘记它。
正如我从 react-form 文档中了解到的那样,您通常会在 reducer 树的根部放置一个 form
reducer,而 redux-form 会将每个表单的状态放入此 form
键中, 基于表单的名称。
然而这会导致一个奇怪的结构,其中某棵树的表单数据在表单键下,而不是与它相关的其他状态数据。在此示例中,应用程序存储来自服务器的可能帐户类型列表,逻辑上应将其与注册表单本身分组。然而,使用 redux-form 它们将位于完全不同的子树中:
signup
/
forms
/ \
/ login
/
root
\ login--signinMethods
\ /
header
\
signup--accountTypes
我更喜欢像这样更有条理的树:
form
/
signup
/ \
/ accountTypes
/
root
\ signinMethods
\ /
login
\
form
redux-form 或任何其他 redux 库是否可行?
尝试使用 reduxMountPoint 选项:
The use of this property is highly discouraged, but if you absolutely need to mount your redux-form reducer at somewhere other than form in your Redux state, you will need to specify the key you mounted it under with this property. Defaults to 'form'.
它的构建方式是这样的(一个 reducer 处理所有表单),不需要在每次需要新表单时都添加一个新的 reducer。您只需添加一次减速器,大部分时间都可以忘记它。