I am working on a react/relay-based content management system. Users can create and modify articles, which are stored on the server. I was wondering what the best way is, to handle the modified state of the article before it is saved to the server. I can think of several different ways to solve this:
1) Uncontrolled inputs
I can populate the input-elements using defaultValue
and not store the state anywhere explicitly. The DOM would be used as my store for the modified data. Once the user hits "save", I collect all fields, read the values and create the mutation.
Pro:
- No local state handling
Contra:
- I can't really know which fields were changed and would need to send all data via the mutation. Or would need some additional logic to create a diff
- It is not possible to update other parts of the view in response to the state changes
2) Copy in the local state:
I could keep the modified article in the local state of the React component and use controlled input fields to keep it synced.
Pro:
- The local state could only have changed fields, so a diff would be easy
- Other parts of the UI can respond to the changes of the local state
Contra:
- This seems to be kind of an anti-pattern because data in the view is not coming directly from the relay. Syncing between local state and relay props might be a source for bugs
3) Server is the new local:
Simply create a mutation for every single change that is made. Using optimistic updates this should also provide a good UX.
Pro:
- Relay is the only source of truth for data
- The state is saved server-side, so there is the backup if the user accidentally closes the browser
Contra:
- This would need a more complex implementation on the server-side to handle cases where the user wants to discard a draft, etc.
- Many mutations triggered
These are three ways to solve this I could think of, but perhaps there are even better ways to solve this.
I have seen that there is a lot of discussion going on, about how to handle local state with Relay, and there might come a built-in solution with a future version of Relay, but I need a solution that works with the current version of a relay.