Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rerendering when field is emptied #545

Closed
jonathanasquier opened this issue Jan 14, 2016 · 11 comments
Closed

Rerendering when field is emptied #545

jonathanasquier opened this issue Jan 14, 2016 · 11 comments

Comments

@jonathanasquier
Copy link

hey,

I'm trying to save a password in a field and empty it in my reducer on success update.
When other fields are dirty the password is correctly emptied, the behavior is the one expected, but if only the password field is dirty (it is empty on form initialize) I believe redux-form (or is it react?) thinks there is nothing to update (as the form is identical, empty password at first, empty password then) so my password field stays populated, and dirty. It there a way to force re-render?

thanks!

@hnordt
Copy link

hnordt commented Jan 15, 2016

I'm almost sure it's related to #547

@jonathanasquier
Copy link
Author

Couldn't make it work by setting the password to empty string, the state when the password is untouched is still the same as when it is emptied.

@erikras
Copy link
Member

erikras commented Jan 20, 2016

Let me see if I understand. The steps to test this are:

  1. Set initial values to {myPass: undefined}
  2. Render <input type="password" {...myPass}/>
  3. Enter some value into the password field.
  4. Call resetForm()
  5. See that password field still has data in it even though it shouldn't.

Is that right?

@jonathanasquier
Copy link
Author

Not exactly, on the 4) step i submit m'y form, i get a response of the
updated user, in which userpass is empty.
As the userpass is the only field changed, both states before and after
submitting are identic. So step 5) happens.

@erikras
Copy link
Member

erikras commented Jan 20, 2016

After you "get a response of the updated user, in which userpass is empty", what do you do with it? Call initializeForm() or pass it as an initialValues prop?

@jonathanasquier
Copy link
Author

My form has my user model as initialvalues on load, so i bet on the user
state update that redux-form reinits the form with new initialvalues?

@erikras
Copy link
Member

erikras commented Jan 20, 2016

And you can't replicate the same behavior on any other form field that is not type="password". Related, maybe, to #370 (comment)?

@jonathanasquier
Copy link
Author

No it's a type="text" actually.

@erikras
Copy link
Member

erikras commented Jan 20, 2016

Per the comment I linked to in my previous comment, I think you might have to dispatch initializeForm() yourself the second time. For all the reasons discussed in that issue.

@jonathanasquier
Copy link
Author

So I've dispached initializeForm as said:

//action
export function updateUser(userId){
    return function(values, dispatch){
        API.updateUser(userId, values)
            .then(function(data){
                dispatch({type : UPDATE_USER_SUCCESS, values : values});
                dispatch({
                    type : 'redux-form/INITIALIZE',
                    form : 'userform',
                    data : values,
                    fields : _.keys(values)
                })
            })
            .catch(function(error) {
                dispatch({type : UPDATE_USER_FAIL, error : error});
            });
    }
};

Not quite fond of it, but it works as expected :)

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants