react rendered more hooks than during the previous code example
Example: Rendered more hooks than during the previous rende
Rendered lesser hooks than the previous render.
Rendered more hooks than the previous render.
In both the cases thing can be like you have a conditional statement calling the same function which returns render from different places like both wrapped in a parent return function:
const parentFunc = () => {
if(case==1)
return function_a();
if (case==2)
return function_b();
}
now function_a() could be a function creating two or one hook suppose useStyle() or anything else
and function_b() could be a function creating no hook.
Now, when parentFunc returns function_a() rendering one hook and function_b() rendering no hook then react will tell you that from the same render function two different renders were returned one with two or one hook and the other with one hook this disparity leads to the error. Error being
less hooks were rendered. And the error is quite obvious.
When cases are reversed and function_b() is returned first cause of the conditionals then react will tell you that from the same render function different renders were returned and error will be .
Rendered more hooks than previous render.
Now, Solution:
Change the code flow like maybe create function_ab() which will ensure all the hooks being used are rendered and in that function:
const function_ab = () => {
if(case==1)
return (<div></div>)
if(case==2)
return (<div>I am 2 </div>)
}