question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update gets triggered on render with initialValues

See original GitHub issue

Are you submitting a bug report or a feature request?

Not sure if it’s a bug or a if it works as intended

What is the current behavior?

Updates are triggered once when then the form is rendered, i.e. even though no changes have been made to the form values.

See https://codesandbox.io/s/0ml4k745x0

What is the expected behavior?

I would expect updates to be triggered only on updates, not by the initial values.

What’s your environment?

    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-final-form": "^4.0.2",
    "final-form": "^4.11.0",
    "final-form-calculate": "^1.3.0"

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:12

github_iconTop GitHub Comments

2reactions
nik-lampecommented, Mar 28, 2019

Interesting. This does not happen, when you specify the field as a regex. And, it gets triggered, but it seems that the changes won’t be applied to the values.

Came across this, because I’m looking for a way to do a initial calculation with the initial values

1reaction
leonardopollycommented, Jun 17, 2021

@cordial, I got to work around this by wrapping createDecorator function in my own really small decorator that returns the original decorator called with form as its argument. In the updates property you can now access form and check if it is pristine. If it is, simply return an empty object 😃

Instead of writing

const myDecorator = createDecorator({
  field: 'fieldA',
  updates: {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
});

you can write

const myDecorator = form => createDecorator({
  field: 'fieldA',
  updates: form.getState().pristine? {} : {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
})(form)
Read more comments on GitHub >

github_iconTop Results From Across the Web

reduxForm - initialValues did not apply after update
I have a form component which get's it's "initialValues" via a "mapStateToProps" function. On the first render everything works quite fine.
Read more >
Core — Jotai, primitive and flexible state management for React
initialValue : the initial value that the atom will return until its value is changed. read : a function that's called on every...
Read more >
The Complete Guide to useRef() and Refs in React
Updating the reference value countRef.current++ doesn't trigger component re-rendering. This is demonstrated by the fact that 'I rendered!'
Read more >
Basic Hooks - React Hooks Testing Library
NOTE: There's a gotcha with updates. renderHook mutates the value of current when updates happen so you cannot destructure its values as the...
Read more >
Final Form Docs – `FieldProps`
If false , format will be called on every render. initialValue. any. Optional. The initial value for the field. This value will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found