简体   繁体   English

React不知道Redux-Form状态更改

[英]React not aware of Redux-Form state change

Our app has a page for administrators to make Quotes for our clients. 我们的应用程序有一个管理员页面,可以为我们的客户制作报价。

A Quote consists of one or more "jobs". 报价由一个或多个“工作”组成。 Eg If a quote is for installing a fridge, and doing yard work the Quote would consist of the following two jobs: 例如,如果报价是用于安装冰箱,并进行码头工作,报价将包括以下两个工作:

  • Fridge Installation -$xx 冰箱安装 - $ xx
  • Yard Work - $yy 庭院工作 - $ yy

There is other metadata associated with a job that warrants a whole other form for creating / editing a single job. 还有与作业相关联的其他元数据,其保证用于创建/编辑单个作业的整个其他形式。

Hence the page for creating / editing a single quote consists of two forms: 因此,用于创建/编辑单引号的页面包含两种形式:

  1. Quote form for handling the currency of the quote, the title of the quote, toggling of jobs, and deleting of jobs 用于处理报价货币,报价标题,切换作业和删除作业的报价表
  2. Job form (in the shape of a Modal) for handling the title of the job, description of the job and a list of items that break down the work to be done for that job 作业表格(以模态的形式),用于处理作业的标题,作业的描述以及分解要为该作业完成的工作的项目列表

I have a top-level container component that renders the following 2 Redux-Form components: 我有一个顶级容器组件,呈现以下2个Redux-Form组件:

    {/* IN CONTAINER COMPONENT */}
    <JobModalForm
      onSubmit={this.saveJob}
    />
    <QuoteForm
      initialValues={quote}
      onSubmit={this.updateQuote}
      updateQuote={this.updateQuote}
      deleteQuote={this.deleteQuote}
      publicQuoteUrl={this.props.publicQuoteUrl}
      linkCopied={() => { this.props.snackbarString('link copied!') }}
    />

Both of these forms are stored in separate parts of the form state: 这两种形式都存储在表单状态的不同部分中:

--form |
       | -- quote
       |
       | -- job

However, these two forms have to communicate with each other. 但是,这两种形式必须相互通信。

All is working except for one thing: 一切都在起作用,除了一件事:

When I update a job (whether pre-existing using initialValues or newly-added) for the first time, the update works: If I change the price on a job, the QuoteForm will render the new price. 当我第一次更新作业(是否预先存在使用initialValues或新添加)时,更新有效:如果我更改作业的价格, QuoteForm将呈现新价格。 This is done through reduxForm's arraySplice 这是通过reduxForm的arraySplice完成的

  // in container component ...
  // onSubmit handler for JobModalForm
  saveJob = (job, dispatch, props) => {

      // if this is an existing job (whether or not it has been created in the db or not)
      // update it
      // else 
      // append the job to the existing quote
      if (job._id || job.fresh) {
         const action = arraySplice('quote', 'jobs', job.index, 1, job)
         dispatch(action)
      } else {
         dispatch(arrayPush('quote', 'jobs', job ))
      }

      dispatch({ type: JobModalActions.TOGGLE_MODAL })
}

However, subsequent updates to ANY existing Fields within QuoteForm's ArrayField do not trigger a render from React. 但是,对QuoteForm的ArrayField中的任何现有字段的后续更新不会触发React的render Eg changing the price on any job after my initial update does not get noticed by React. 例如,在我的初始更新后更改任何工作的价格不会被React注意到。

Redux-Form successfully dispatches an action and the store's form key does contain the updated fields . Redux-Form成功调度一个动作商店的form键确实包含更新的字段

Curiously enough, creating any number of jobs does work (by way of arrayPush as seen above). 奇怪的是,创建任意数量的作业确实有效(通过如上所示的arrayPush )。

I'm totally stumped and need some help on figuring out why form submissions for JobModalForm only work for new jobs. 我完全难过,需要一些帮助来弄清楚为什么JobModalForm表单提交仅适用于新工作。


Appendix 附录

Here is an example replicating the issue: https://www.webpackbin.com/bins/-KgoQMIyvxdJb7Pvh0eu 以下是复制问题的示例: https//www.webpackbin.com/bins/-KgoQMIyvxdJb7Pvh0eu

Code snippets 代码片段

Versions of packages: 包的版本:

"react-redux": "^4.4.1",
"redux": "^3.3.1",
"redux-form": "^6.5.0",
"redux-form-material-ui": "^4.1.2",

JobModalForm JobModalForm

export default connect(mapStateToProps, mapDispatchToProps)(reduxForm({
  form: 'job',
  enableReinitialize: true
})(JobModal))

QuoteForm QuoteForm

export default connect(null, mapDispatchToProps)(reduxForm({
  form: 'quote'
})(QuoteForm))

I realized this is a shallow comparison issue within Redux-Form - not my source. 我意识到这是Redux-Form中的浅层比较问题 - 而不是我的来源。

Here is the issue. 是问题所在。 As you can see, Field Array does shallow comparison without allowing the user to allow for deep comparison. 如您所见,Field Array在不允许用户进行深度比较的情况下进行浅层比较。

There's actually a PR within the repo ( link ) that resolves this very issue. 回购( 链接 )中实际上有一个PR来解决这个问题。

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

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