(no title)
thomasikzelf | 4 months ago
<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.
dsego|4 months ago
thomasikzelf|4 months ago
cleaning up listeners or cleaning up DOM nodes is rarely needed. Just remove the component element and the browser cleans up automatically.