通过更改 react-intl 包装器道具切换语言时,我的 redux-form 被重新初始化 [英] My redux-form is reinitialized when switching language by changing react-intl wrapper props

查看:24
本文介绍了通过更改 react-intl 包装器道具切换语言时,我的 redux-form 被重新初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用程序包装在来自 react-intl v2,像这样:

I'm wrapping my app inside an IntlProvider from react-intl v2, like this:

<IntlProvider locale={this.props.lang} messages={this.props.messages}>

而且我有我的 react-form 表单.一切都很棒.

And I have my react-form forms down the tree. Everything is great.

但是如果我开始填写表单,并且我决定更改 UI 的语言(通过监听将更新我的 langmessages props 通过我的 redux store),表单正在被重置:-/

But if I'm starting to fill up a form, and I decide to change the language of the UI (by listening to an action that will update my lang and messages props through my redux store), the form is being reset :-/

我看到操作 redux-form/INITIALIZE 在语言改变时被触发.

I see the action redux-form/INITIALIZE being fired when the lang changes.

即使我将 destroyOnUnmount: false 传递给 reduxForm() 也会发生这种情况.

And it happens even if I pass destroyOnUnmount: false to reduxForm().

在不受控制的表单字段中不会发生这种情况.

It did not happen with uncontrolled form fields.

有什么想法吗?

我的代码:App.js + 注册表格

推荐答案

嗯... 您正在更改 initialValues 属性,因此它正在初始化表单数据.initialValues 旨在用于来自要编辑的服务器的规范数据(例如加载的记录),而不是真正用于从组件外部更新单个表单值.

Hmm... You are changing your initialValues prop, so it's initializing the form data. initialValues is intended to be used for canonical data from the server to be edited (e.g. a loaded record), not really for updating individual form values from outside the component.

也许你可以使用 plugin() API 注意到语言更改操作(是否通过 Redux 更改?)并更新表单中的 lang 值?

Perhaps you could use the plugin() API to notice the language change action (is that being changed via Redux?) and update the lang value in your form?

这篇关于通过更改 react-intl 包装器道具切换语言时,我的 redux-form 被重新初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆