1

I am confused about why I cannot clear <input> field values using this simple pattern with React:

onComponentDidMount: function () {
    this.clearFields();
},

clearFields: function () {
    document.getElementById('username_field').value = '';
    document.getElementById('password_field').value = '';
},

I don't think it's a problem with React, I think there some other issue at hand but I am not sure what's going on. But the fields definitely do not clear out. Later on, I can call this.clearFields() and that function does work as expected, but not when the component first mounts.

1 Answer 1

2

The correct React lifecycle function is called componentDidMount, not onComponentDidMount

However, you don't want to do it this way if the inputs are also rendered with React. It's usually better and more relevant to the application to change values stored in the state, and let the render function deal with setting the value of the input fields.

Sign up to request clarification or add additional context in comments.

1 Comment

agreed you should be doing this in getInitialState, and this.setState({}); when it comes to react

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.