All you need is the event handler. The event handler can be assigned to a node's event property directly, but if this is for a commercial site with lots of third party analytics/spyware you need to use a event listener instead because those third party libraries will overwrite your event assignments. The event handler receives an event object as an argument with a reference back to the given DOM node.
All the business logic should be stored in a single location representing a given business function, typically far away from the browser logic. It makes sense when you write the code without using a framework. If you write the code and try to make it look like a framework it will look like a framework, unsurprisingly. If you don't do a bunch of framework component insanity it will not look like a framework.
This is a person problem, not a technology problem.
const myHandler = function (event) {
console.log(event.target);
};
// this is simple and easier to troubleshoot,
// but you can only directly assign one value to an object property
myNode.onclick = myHandler;
// for pages with JavaScript code from third party domains
// use event listeners instead
myNode.addEventListener("click", myHandler);
When leaving frameworks you have to stop thinking in terms of components, isolated code islands that exist only in their own isolated world. Instead think of areas of the page as parts of a larger whole. You can manage each part independently or not. You wouldn't program outside the browser using something like React, so your programming inside the browser should reflect how you would build an application if the browser weren't there. <select id=dropdown>
<option value=a>a</option>
<option value=b>b</option>
</select>
<select id=a style="display: none">...</select>
<select id=b style="display: none">...</select>
<script>
const $ = name => document.querySelector(name)
$('#dropdown').addEventListener('change', ev => {
$('#a').style.display = ev.target.value == "a"? "block" : "none"
$('#b').style.display = ev.target.value == "b"? "block" : "none"
}
</script>
vs const [showing, setShowing] = useState(null)
const handleChange = ev => setShowing(ev.target.value)
let other
if(showing == "a") other = <select>...</select>
if(showing == "b") other = <select>...</select>
return <>
<select onChange={handleChange}>
<option value=a>a</option>
<option value=b>b</option>
</select>
{other}
</>
some notes:- The complexities of both of these tiny pieces of code is similiar (I would say)
- React needs to load a big bundle
- React spits out a large stacktrace with react internals if something goes wrong
- React is slower
- React code cannot be easily stepped through with the debugger
- React needs a build step
- React performance is very unpredictable (this is more of a problem with many elements and dynamic code)
Your next question might be what you do once your form grows huge. See my other answer to @iliaznk how I handle that.
cleaning up listeners or cleaning up DOM nodes is rarely needed. Just remove the component element and the browser cleans up automatically.