How to wait for useState to finish before setting useRef value?
I have two following form elements that do similar things. Get value using useRef and adding it to an array of elements using useState. First one works perfectly. It sets the value and then removes everything in input element and unfocuses it. However with the second one I have to choose between pushing an empty string or letting the input element retain its value. I'm guessing that is because useState is async and I just got lucky in the first one because it is a faster operation and it executed before input element was reset. What is the correct way to use these two hooks together with reseting input element afterwards.
Solution
Yeah you've got the right idea.
I think the important part would be avoiding the capture of the DOM element. Using a reference to the string itself should work fine.
newSubCatInput.value is captured by reference in the anonymous function (closure?, lambda? not sure what js calls em)I think the important part would be avoiding the capture of the DOM element. Using a reference to the string itself should work fine.