Planning Data and State

Data is the information an application uses. State is the information describing its current condition: selected preferences, items in a list, or whether a request is loading.

Choose a suitable structure

Use a single value for one piece of information, an array for a collection, and an object for related properties. An array of objects can describe a collection whose entries each have several properties.

Create a small data inventory before coding:

InformationStructureSourceChanges during use?Saved after refresh?
A display preferenceStringUser selectionYesIf required
A catalogueArray of objectsSupplied data or APIDepends on the applicationOften unnecessary
Current selectionID or other valueUser actionYesOnly if required

Choose stable IDs when names might change. Decide the expected types and initial values. Keep user data separate from DOM element references; an HTML element reference is not the data you store as JSON.

Decide what persists

Save only what must survive refresh. A count can often be calculated from an array instead of stored separately. If using an API, identify the fields needed and the behaviour when a request fails. Local Storage is browser-local and is not a secure store for secrets.

Checklist

  • [ ] I identified the values, arrays, and objects I need.
  • [ ] I know where each value comes from and when it changes.
  • [ ] I chose initial values and stable identifiers where needed.
  • [ ] I separated application data from DOM references.
  • [ ] I identified what is saved, fetched, or calculated.