Search code examples
reactjsreduxreact-fullstack

React-Redux ideal interaction with a database


If there is a complex Redux store for determining the states of many components throughout the app.

What is the best pattern for when to save things to the DB? I see pros and cons to different approaches, but I am wondering what is standard for applications with a complex UI?

  • Save the store to DB every time a change is made. (Makes it difficult chasing lots of instant vs. async processes... Either lots of loading states and waiting or juggling the store and the DB separately.)

  • Autosaving every now and then... (Allows the store to instantly determine the UI, faster... With occasional loading states.)

  • Manual saving... Ya, no thanks...


Solution

  • I recommend saving automatically every time a change is made, but use a "debounce" function so that you only save at most every X milliseconds (or whatever interval is appropriate for your situation).

    Here is an example of a "debounce" function from lodash: https://lodash.com/docs/#debounce