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

Attach change listeners directly #21

Open
eliasmalik opened this issue Sep 27, 2019 · 1 comment
Open

Attach change listeners directly #21

eliasmalik opened this issue Sep 27, 2019 · 1 comment

Comments

@eliasmalik
Copy link

React.useEffect(() => {
const updateCategory = event => setCategory(event.target.value);
const dropdown = document.querySelector("#Dropdown");
dropdown.addEventListener("change", updateCategory);
return () => dropdown.removeEventListener("change", updateCategory);
}, []);

You can set the change listeners directly on the JSX component, then you don't need to worry about managing listeners manually on mount/unmount.

<select onChange={changeHandler}>
  {...}
</select>
@eliasmalik
Copy link
Author

Same thing here:

<form className="lets-go-form">
<input placeholder="Enter a username" className="usernameInput" type="text" />
<button className="letsGoButton">Let's Go</button>
</form>

You can attach the change listener to the enclosing form element and it'll let you handle the changes of all child input elements if that's better for your use case too.

<form onChange={changeHandler} onSubmit={submitHandler}>
  <input type="text" name="name" />
  {...}
</form>

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