dimanche 23 avril 2023

Is using shared mutable state ok for small functionality

Recently I am working on a feature that let's a user change his data, so I keep a copy of all his changes until user presses "Save" and then that copy is persisted. It looks something like this:

const initialData = {email: "something@gmail.com", firstName: "John", lastName: "Smith"}
const currentData = {email: "something@gmail.com", firstName: "James", lastName: "Smith"}

In this example user just decided to change his first name.

I need to have this information about current data in two places:

  1. In Save callback, so that when user presses Save button it is persisted
  2. In "UpdateUI" functionality so that change can be visible in the UI, and to change "currentData" value on input change.

Doing this requires to have some shared state where both "Save" and "UpdateUI" can have access to the current and latest of user changes.

Since I read a lot about how shared mutable state is root of all evil, I was wondering is using shared state for such small functionality alright, or is there some better way?

Aucun commentaire:

Enregistrer un commentaire