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:
Contra:
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:
Contra:
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:
Contra:
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.
Short answer: It depends, I prefer #2 solution
Solution #2 is an advanced version of solution #1, so #1 is passed. But #1 of course has it's case, most old sites do that.
Solution #3 will keep the server busy, that will either raise your cost for server or slow down user experience when your business scales, so #3 is passed. But as an internal system or some business meant to be used by less user, #3 become good choice.
If the user accidentally closes the browser
Solution #2 still can use an interval to sync it's content to server, surely you've seen auto save in xx seconds
many times. Or you can use localstorage, it will not save cross computer, but it will save every bit of your change, even every history with zero sync lag.
Syncing between local state and relay props might be a source for bugs
I should say this is not only happen for #2, if some other change the article at the same time
For #1, your modification will be overwritten, and you can never find a clue where your modifications gone
For #3, your content is changing all the time, can you still work on it?
So if there is such case, you need to add version control logic right? If no such case, your local state is single truth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With