简体   繁体   English

“切换”属性不起作用(redux-form-material-ui)

[英]“toggled” attribute not working (redux-form-material-ui)

I'm trying to use Toggle from redux-form-material-ui : 我正在尝试从redux-form-material-ui使用Toggle:

import { Toggle } from 'redux-form-material-ui'

It works ok updating the toggled value to the store on its onChange : 它可以正常工作,在onChange上将切换后的值更新到存储中:

<Col xs='3'>
    <h3 className="title-page">Parceiro</h3>
    <Field name="possui-parceiro" component={Toggle} label="Possui parceiro?" />
</Col>

Problem is: I make a call to some API and I need to update the value of this toggled "programatically". 问题是:我调用了一些API,并且需要“以编程方式”更新此切换的值。 Theoretically, I can use the toggled attribute as stated here , but this just doesn't work: 从理论上讲,我可以使用此处所述的toggled属性,但这是行不通的:

<Col xs='3'>
    <h3 className="title-page">Parceiro</h3>
    <Field name="possui-parceiro" component={Toggle} toggled={this.state.someBloodyState} label="Possui parceiro?" />
</Col>

Which leads me to believe that in this case redux-form is just on the way of the update / manipulation process, forcing me to somehow update the form on the store to toggle the value, and it looks messy to dispatch such action. 这使我相信,在这种情况下,redux-form只是更新/操作过程的途中 ,迫使我以某种方式更新存储上的表单以切换值,并且派遣此类操作看起来很混乱。 Anyway, how do you proceed in such cases? 无论如何,在这种情况下您如何进行?

I would store the API result in the redux state, read it from your mapStateToProps and pass it to the component in the property initialValues setting enableReinitialize: true . 我会将API结果存储在redux状态,从mapStateToProps读取它,并将其传递给属性initialValues设置enableReinitialize: true的组件。

These 2 properties are the way of redux-form to change stuff programmatically "later on" after the form already rendered. 这两个属性是redux-form在已呈现的表单之后以编程方式“稍后”更改内容的方式。

Otherwise , if you can fetch your data before even rendering the form, you can just use initialValues without enableReinitialize . 否则,如果您可以在渲染表单之前就获取数据,则可以仅使用initialValues而不使用enableReinitialize

Another way is to use the change function provided by redux form 另一种方法是使用redux表单提供的change函数

More info in the docs 文档中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM