简体   繁体   English

Redux形式v6 isDirty(和isPristine)选择器在状态更改时不会触发重新渲染

[英]Redux-form v6 isDirty (and isPristine) selector not triggering re-render on state change

I am trying to detect whether the form state is dirty from a parent component so I can only display a submit button if the form values have been changed.. 我正在尝试检测父组件的表单状态是否脏,因此如果表单值已更改,我只能显示一个提交按钮。

The initial render shows false as expected but changing the values does not trigger a re-render with the new vlaue.. 初始渲染按预期显示为false,但是更改值不会触发使用新vlaue的重新渲染。

import React from 'react'
import { isDirty } from 'redux-form'
import { connect } from 'react-redux'

<Parent>
  { props.isDirty ? <SubmitButton /> : null }
  <Form {...etc} />
</Parent>

const mapStateToProps = state => ({
  ...
  isDirty: isDirty('myForm')(state),
})

export default connect(mapStateToProps, null)(Parent)

Console logging props.isDirty shows that at the initial render of <Parent/> isDirty is false. 控制台日志props.isDirty显示在<Parent/>的初始渲染中isDirty为false。 However, changing the values does not trigger a re-render of <Parent/> with the new value. 但是,更改值不会触发使用新值重新渲染<Parent/>

Update: 更新:

After further investigation, I think this is a bug: 经过进一步调查,我认为这是一个错误:

const mapStateToProps = (state) => {
  console.log(isDirty('myForm')(state)
  return {
    ...
    isDirty: isDirty('myForm')(state),
  }

Here, changing form values triggers mapStateToProps as expected but isDirty is always false. 在这里,更改表单值会按预期触发mapStateToProps ,但是isDirty始终为false。

Doh! h! Not a bug. 不是错误。 I was importing the wrong, mutable isDirty selector.. 我正在导入错误的可变isDirty选择器。

import { isDirty } from 'redux-form/immutable'

..solved the problem ..解决了问题

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

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