State management: combination of global and local states? #857
-
Hello everyone 👋 , I'm currently trying to refactor our state management in our flashcard App. Up until now we had, among other states, an isDelete state (boolean) locally and an isEdit state (also boolean) globally. Because the isEdit state could not be toggled without exception, we were advised to reconsider whether a boolean was really the right choice. Actually, I decided to solve it with strings and wrote the following useState and function in the
I used "default" or "edit" in the pages and components, depending on the situation, and that works well. What doesn't work is "delete". I simply adapted the system as above and set the actionMode to "delete". But then the state is global (not local in the I keep the global actionMode state and initially only use it for "default" and "edit" (maybe later for other things). I also keep the local "delete" state (that was actually practical as a boolean). I change the (global) actionMode state so that I pass objects, e.g. {mode: "default", id: null} or {mode: "edit", id: null} or {mode: "delete", id: flashcardId}. I don't think either is ideal, it has different advantages and disadvantages. The first solution is inconsistent because I manage the delete mode somewhere else than the others. But it is simpler and probably less prone to errors. What would you recommend? Or do you have a completely different idea? This is my refactoring branch. (The link is a PR, but it's not finished yet, of course.) Thanks in advance for your thoughts. 🌻 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @eneben ✌️ Thanks for sharing your progress and thoughts on refactoring the state management. It looks like you’ve already made some important decisions, but you’re grappling with how best to handle the delete mode – and that's actually a very good question. Let's break it down Global State for All ModesUsing a global 👍 Pros of a global state for all modes:
👎 Cons of a global state for all modes:
Local State for Delete ModeGiven that the delete action is specific to individual flashcards, it might be more practical to manage this state locally within the 👍 Pros of a local state for delete mode:
👎 Cons of a local state for delete mode:
My RecommendationWhile there are trade-offs, I would lean towards keeping the delete mode as a local state within the If you find that managing modes in two places becomes problematic in the future, you could always revisit the idea of centralising state management, but for now, the simplicity and clarity of local state for delete seems more beneficial to me. I hope this helps! Looking forward to seeing how the refactor turns out. |
Beta Was this translation helpful? Give feedback.
Hello @eneben ✌️
Thanks for sharing your progress and thoughts on refactoring the state management. It looks like you’ve already made some important decisions, but you’re grappling with how best to handle the delete mode – and that's actually a very good question.
Let's break it down
Global State for All Modes
Using a global
actionMode
state, as you’ve implemented, works well for modes like "default" and "edit" because these states need to be consistent across the entire app. However, extending this approach to the "delete mode" has caused all flashcards to enter the delete state simultaneously, which isn’t the desired behaviour. 🫨👍 Pros of a global state for all modes: