Handling Stale State Values in Event Handlers with Editor.subscribe #6156
Unanswered
trentduncan
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
I'm working on integrating this feature into our codebase and have encountered some interesting behavior that I'd like to get feedback on from others.
Problem:
When setting up the on change event listener (
editor.subscribe("change", handler)
), the handler function captures state values at the time the handler is instantiated. This means that if any state values change after that, the handler will still reference the stale state values it captured during initialization.Here is an example of the problem:
Potential Solution:
The only solution I can think of is to use refs for the state values. However, this feels like a "sloppy" solution because it leads to splitting state and this becomes a problem as more state values are required by the onChange handler. This can quickly become hard to maintain and error-prone.
I'd love to hear how others are solving this issue, or if there's a better approach to ensure that the event handler always has access to the most up-to-date state values.
Beta Was this translation helpful? Give feedback.
All reactions