Generic object edition with dirty state management #2339
Replies: 1 comment 2 replies
-
Hey, I see 2 issues here. Instead of the 'god object' you could benefit from the bottom-up approach, so you wrap each of the properties to a separate "molecule" which would have separate atom for dirty, value, etc. You could benefit from the form-atoms library, where on top of Currently I don't remember if there are examples how to dynamically initialize a form from a JSON, but the library does support dynamic fields, so you won't get stuck. |
Beta Was this translation helpful? Give feedback.
-
Hello! I am currently working on a project that contains a generic react component whose goal is to take any json object and allow a user to:
Since the structure of the json object can vary, the component is used as a wrapper and only provide utility function through a React Context. as of right now the context contains:
Because every time the user updates a field in the object, it changes the context and re-render the whole wrapper, we want to replace the current Context implementation with Jotai. I have already created a prototype that works, but I have never worked with Jotai before so the point of this thread is to see if anyone can find any issue or suggestion with our implementation.
Entity Atoms and utility functions
Dirty Atoms and utility hooks
This is a first draft, so some stuff like typings and naming are not perfect yet, but I just wanted to know if I was going in the right direction.
Explanation
My editable object and initial entity are both inside a singular atom, I create an utility hook called
useProperty
that acts like a useState for a singular property of the editable object which can be used inside react components.My dirty state is managed by an atom family which creates a boolean atom for each property of my object, I created a special atom which checks every atom in the family to check if the entity as a whole is dirty or not and also a custom hook for checking if a specific property is dirty or not.
Example:
This is my first time creating a thread like this so if I forgot something like putting a tag or something like that don't hesitate to tell me!
Beta Was this translation helpful? Give feedback.
All reactions