DEV Community

Discussion on: Building a chat app with React Hooks, a pragmatic example

Collapse
 
iwaduarte profile image
iwaduarte • Edited

I am a little bit late for the party but if you do not figure it out by now. I will shed some light since the author did not.

When they are saying to call the hooks inside loops, nested functions or conditions does not mean that they are not there for being used in map functions. They are more concerned with the fact of the order of hooks and especially with cases where there is no guarantee that you can preserve the state therefore maintaining the order.

One case is with if statements:

if(!userState) const [user,setUser] = useState('guest'); // order here can not be preserved because it rely upon the userState variable

I do not have a good example about nested functions or for examples but again the order has to be preserved.

Collapse
 
flrnd profile image
Florian Rand • Edited

Oh pretty cool! This has been wandering my thoughts some time, finally some light indeed! thanks!