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

查看:96
本文介绍了通过更改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的语言(通过侦听将通过我的redux存储更新我的langmessages道具的动作),表格正在重置:-/

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 + 注册表格

推荐答案

Hmm ...您正在更改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天全站免登陆