by spanhandler · 6y ago · 0 comments
The result is that practically no-one wants to write in whatever a "JS native" paradigm might look like. Everyone wants to write it like C, or like Java, or like OCaml, or whatever. Mind, I don't think it's a bad thing people avoid writing "JS native" style, because I certainly don't want to be handed a codebase in which anyone's done anything remotely interesting with prototypal inheritance—it's just something I find interesting about the community of such a popular language.
const [username, setUsername] = useState('')
const onChange = event => setUsername(event.target.value)
const onSave = () => dispatch(saveUsername(username))
return (
<div>
<input value={username} onChange={onChange} />
<button onClick={onSave}>Save</button>
</div>
)
This code contains two closures const todoItems = []
addTodoItem.addEventListener(event => {
todoItems.push(event.target.value)
renderTodoItems()
})2. Where are the closures and what's the necessity to use them?
2. onChange and onSave are both closures.
It isn’t technically necessary to use closures, but you need to find the correct setUsername from onChange, and since setUsername isn’t a global variable, closures are the natural and easy way to do it.
1. https://overreacted.io/react-as-a-ui-runtime/
Where else would you propose putting UI rendering logic?
Hooks have shown that Super Serious programming can also be comedy, which is pretty cool.