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

Avoid accessing the DOM directly #4

Open
oliverjam opened this issue Sep 18, 2020 · 0 comments
Open

Avoid accessing the DOM directly #4

oliverjam opened this issue Sep 18, 2020 · 0 comments

Comments

@oliverjam
Copy link

let newValue = document.getElementById("username").value

Generally it's a good idea to let React manage the DOM. This means avoiding stuff like querySelector if you can.

There are two other ways I'd recommend to getting the value of inputs:

  1. Use a submit handler on the form element. The submit event object will contain all the input's values (event.target.elements.inputName.value). As a bonus submit events fire when the user hits "Enter" in an input (which won't work if you're just listening for clicks on the button)
  2. Use a controlled input to keep the value in state. That way you always have the value available as a JS variable in scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant