Potential alternative Hook APIs
Sometimes, it would be nice to define Hooks conditionally or in loops, but why did the React team decide to implement Hooks like this? What are the alternatives? Let’s go through some of them to get a feeling for the trade-offs involved in making this decision.
Named Hooks
We could give each Hook a name, and then store the Hooks in an object instead of an array. However, this would not make such a nice API, and we would also always have to think of unique names for Hooks:
// NOTE: Not the actual React Hook API
const [name, setName] = useState('nameHook', '')
Additionally, there are unresolved questions: What would happen when a conditional is set to false or an item is removed from a loop? Would we clear the Hook state? If we do not clear the Hook state, we might be causing memory leaks. If we do clear it, we might be unintentionally discarding user input.
Even if we solved these problems, there would...