



A common approach in React is to use boolean flags to keep track of state. For example, imagine we made a component that fetched an image. It could look like the following:
const MyImageFetcher = () => {const [isFetching, setFetching] = useState(false);const [isSuccess, setSuccess] = useState(false);const [isError, setError] = useState(false);...
A disadvantage of this is it results in untended states. The intended amount of states for the component is 4; idle, fetching, success and error. The actual number of states produced from the above approach is 23 = 8. Changing the approach from boolean flags to named states can fix this:
type State = 'IDLE' | 'FETCHING' | 'SUCCESS' | 'ERROR'const MyImageFetcher = () => {const [state, setState] = useState('IDLE');...
Intended number of states = 4. Actual number of states = 4.
When state management requirements become complex, finite-state machines are the best approach to UI state management.
“React devs: if your app logic is super simple and only requires fetching + displaying data (CRUD), the only state management you really need is React Query + *maybe* useState. Consider state management libraries when the logic gets more exciting than that. The second your app needs shared state, multi-step user flows, stateful logic, advanced user interactions, etc., your app graduates from "simple" to "complex"”

Currently, XState is the most popular JavaScript finite-state machine library.
signIn, name the event signInButtonClicked.assign.
Example, when an action changes context.email, name the action assignEmail.Storing state from a previous session and restoring it can improve the UX (User Experience). Different approaches to storing state:
Although any state stored here will not be available on different devices, it can be useful for websites that don't require sign-in (and therefore can't send state to be stored in a database).
Example, a Real Estate website that shows houses available to purchase.
If it had a min / max price filter, the values set could be stored in localStorage and retrieved / applied each time the user returns to the website.
Similar to local storage except anything stored here will be cleared when the page session ends. Survives page reloads and restores.
Key / values stored in a URL. Useful when there is a requirement for a user to send a page address to another user in its current state.
The most feature rich and expensive storage approach.
Have any feedback about this note or just want to comment on the state of the economy?

