Hooks can't be defined inside an array.
I wrote an article recently about the rendering order of hooks. https://windmaomao.medium.com/understanding-hooks-part-4-hook-c7a8c7185f4e
It really goes down to below snippet how Hooks is defined.
function hook(Hook, ...args) {
let id = notify()
let hook = hooks.get(id)
if(!hook) {
hook = new Hook(id, element, ...args)
hooks.set(id, hook)
}
return hook.update(...args)
}
When a hook is registered, it requires an unique id, as in line notify()
. Which is just a plain i++
placed inside the Component where the hook is written inside.
So if you have a fixed physical location of the hook, you have a fixed id
. Otherwise the id
can be random, and since the render Component
function is called every render cycle, a random id
is not going to find the right hook in the next render cycle.
That is why if
can not be written before the hook statement either. Just try
const Component = () => {
const b = true
if (!b) return null
const [a] = useState(1)
}
You should get similar error.