A Tool That Disappears When the Scene Changes: Revisiting Drawer Save Timing
Drawing on problems from calling getData on an AlgeoMath tool during scene transitions, this separates React cleanup functions from the responsibility of saving user data.
Joohwan Yu · Founder, Full Stack Developer
Toris · Published
Contents
In the educational viewer’s Drawer, I tried to save the tool’s data when the scene changed. The flow was to read the current content with tool?.api.data.getData() and restore it with setData() in the next scene. The problem was that when the scene number changed, the tool might already be invalid at the moment I tried to read the data.
No Errors, but No Save Either
Optional chaining lets you avoid an exception when the tool is missing. But it doesn’t solve the fact that the tool is missing in a situation where a save is needed. If the call is silently skipped, the screen moves on, and the user may think their previous work was kept.
The question I asked at the time was “why isn’t getData() being called?” Looking at it again now, “who starts the scene transition, and who should guarantee the save before it?” was the more accurate question. The moment a component disappears and the moment a user’s work is committed are not necessarily the same.
Don’t Treat Cleanup and Saving as the Same Job
The cleanup function in useEffect is needed for tidying up subscriptions or connections. But cleanup also happens when dependencies change and the effect re-runs. So if you hand all important saving responsibility to the cleanup function, it becomes hard to trace which tool in which scene you’re reading.
If I were designing it again now, I would bundle the scene transition into a single explicit action. The order would be: secure the current scene ID, read a snapshot from the live tool, confirm the save result, then move on. With an asynchronous save, rapid consecutive clicks also have to be considered, because you have to decide whether to allow a second navigation before the save finishes.
If I Were to Verify Again
The situations to check are pressing next and previous quickly, the tool not being ready yet, and the data read failing. In particular, I would distinguish “no tool” from normal empty data. Having saved an empty shape and having failed to read the tool must not be treated as the same value in the restore step.
Rereading the question from back then, I think I was leaning too heavily on the component’s lifetime for saving data. For important data, it is easier to understand when the responsibility is made visible inside the user’s transition action rather than at the moment the component disappears. I would start by spelling out what to read, and where to leave it, while the tool is still alive.
Technical reference: Cleanup behavior of React useEffect
Advertisement