redux-form: Calling Field's input.onChange(undefined) does not set field value to undefined.

Hey there, I’m trying to reset a form field to be undefined but it doesn’t seem to work. Here is a codesandbox showing it not working:

https://codesandbox.io/s/5w72x5rpmx

It does work passing null, but I am confused why undefined doesn’t work. I couldn’t find any mention of why this would be the case in the docs but maybe I missed something.

Besides this behavior being confusing, I would especially love it to be set to undefined (and not null or false) in my case because I often set certain default values for my form values using the

const {input: {value = "hey"}} = this.props

Which only works if the value gets reset to undefined.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 12
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Maybe we should not care about initial value at all, and let user set his desired value.

The issue is still there in redux-form@8.3.5. Not resolved. We can try to set the field value to null as of now. In this way null value will go to API. But I would still love to see setting the value to undefined to clear the field in this case.

@iamandrewluca I have 8.2.6.

I am addressing the issue pointed in this comment:

Looks like we still can’t set undefined for fields with initial values. Sandbox here This is caused by shouldDelete check into createDeleteInWithCleanUp:

https://github.com/erikras/redux-form/blob/87beada1e8a42c62e66c297f1651870f8fe6f254/src/deleteInWithCleanUp.js#L33-L35

https://github.com/erikras/redux-form/blob/de8beb08e888f99c30753fee93768c4e7e0788c3/src/createReducer.js#L44-L54

As @YauheniKhadanionak pointed, shouldDelete would return false if there is initial value for field. https://github.com/redux-form/redux-form/blob/v8.2.6/src/createReducer.js#L52-L56

So deleteInWithCleanUp not work if initial value presented.

Btw for what reason there are checks for initialValue in change handler?

Same problem here.

In redux-form-input-masks, we use normalize to create input masks, and we’ve been returning an empty string to clear the Field’s value to undefined, but this behavior is not detailed on the docs.

Also, using empty string to clear the Field will not work if the input has an initial value. This is some odd behavior: why should the initial value determine how the change action behaves for empty strings? This is caused by the line of code below:

https://github.com/erikras/redux-form/blob/a97606fee0ed365903fff80cc93956fe2313a3ce/src/createReducer.js#L232

I’m okay with empty string setting undefined on the Field’s value (if that’s well documented), but from the user’s perspective it just makes no sense that initial value would play a role in this case.

What is the use for the initial === undefined on the mentioned line of code? Couldn’t we just remove it and improve documentation?


Here’s the problem that I have with this behavior (sandbox):

issue37

I think this issue should be reopened, it’s crucial.

@Olliebaba Destructuring default values only work when the value is undefined. For example sometimes in my app I have a form value getting defaulted to an empty array and then use logic like array.length. Currently I can’t unset that value because onChange with undefined doesn’t work and if I did an onChange with null then that value would not be defaulted to an empty array and my app crashes.