I am wondering if there is any good way to create a React component instantly (not having to wait for the setState to execute a re-render), or any other ideas that could solve the following problem:
Say I am rendering a list of Text components that are all contenteditable <p/>'s that the user can type in. When the user presses Enter, a new Text is created under the current one, and the caret then moves to this new component, allowing the user to type.
I have implemented this and it is working, but adding a new Text to the rendered list with setState() is too slow, and if the user presses Enter and begins typing immediately, some text is added to the wrong node or lost because the re-render does not happen quick enough.
I need the user to be able to press Enter and type immediately without losing any text, and the user should also be able to press Enter many times in quick succession to create multiple new Text components, which also does not work for the same reason.
The solution should not only not lose any of the typed keys, but also display to the user the typed text (and newly created Text components) as quickly as possible.
Here is an extremely contrived example. Go to the end of one of the texts, press Enter and quickly type. You will see that some of the text gets added to the first component instead of the second.
https://stackblitz.com/edit/react-xbr9pf
Example output showing the order in which keys were pressed:
Thank you!

There are a few problems with your implementation, including
that = this, constantly getting elements byids instead of by react refs, hacks withtextContent = textContent, trying tofocusbefore setState has kicked in and so onI've done some minimal edits to your example like changing
keyuptokeydown, usingpreventDefaultandrefs instead of finding element by id. It now works as expectedhttps://stackblitz.com/edit/react-11g7ec