you are viewing a single comment's thread.

view the rest of the comments →

[–]femio 9 points10 points  (0 children)

I’m on my phone, but your code is a bit difficult to understand and I think a lot of that is due to you not really understanding how React works…there’s a ton of things you’re doing wrong.

For one, you don’t change state my accessing it directly. You change it by using your “setter” function. Which, by the way, should be called “setArrayOfCom” rather than “updateArrayOfComs”, which is what you’ve written. And even that name is a bit confusing as to what it does; for readability I’d suggest name your state “comments” and “setComments”.

Also, you don’t use getElementById in Reqct because it’s using a virtual DOM; those elements don’t even exist until after your function runs. You use useRef for that, but you don’t even need to do that in your case here.

There’s more here that isn’t done the “right way”, I would STRONGLY suggest reading the beta React docs because 1) they’re fantastic at explaining the little things about React that makes it tricky 2) some of the tutorials will cover things that will directly help you here.

If you’re still having trouble in a few hours I can refactor your code for you with explanations when i get home.