Deleting a React view can be better than simplifying its state
A date-range switch looks like a harmless addition to a React component. In practice, it can introduce a second navigation model that has to stay in sync with the first. This happened in Twinly , a React and TypeScript baby tracker designed for twins. Its home screen already provides a daily summary. A separate timeline temporarily offered both daily and weekly views. The weekly timeline answers…
A React component can suffer from unnecessary complexity when multiple navigation modes are added. Twinly, a TypeScript baby tracker for twins, experienced this issue after introducing a dual daily and weekly view. The original timeline already provided a summary screen and a separate timeline for daily and weekly views. The weekly timeline served a distinct purpose, showing how care events evolved over several days. The daily view overlapped with the home screen, highlighting the impact of adding another navigation model.
The challenge arose from managing three pieces of navigation state: display mode, selected day, and the start of the selected week. The component's complexity increased with each change in date navigation, as it required considering both modes' transitions and reset behavior. This complexity stemmed from additional buttons and the need to account for both views in the code.
React's documentation advises against redundant state, but it does not dictate whether a product needs both navigation modes. In Twinly's case, the design decision was to maintain today's summary on the home screen and revert the timeline to a weekly-only view. This change eliminated the need to maintain a selected day or display mode. The component retained other state, such as the selected event, but focused on simplifying the date navigation process.
The review process involved deleting the unused daily timeline component, CSS, and related behaviors, rather than merely hiding the toggle. This approach preserved the existing weekly navigation, event markers, tap-to-edit behavior, and home-screen daily summary. The backend, persistence logic, and data model remained unchanged. The pull request underwent successful TypeScript checking, build, and deployment to a development environment before being merged.
Before adding a new display mode, it is essential to identify the specific user question that mode addresses. If another surface already answers that question, consider enhancing that surface instead. When removing a mode, carefully examine the corresponding state, effects, conditional rendering, styles, and interaction paths. Preserve the data and behaviors that remain useful. A smaller state machine and fewer unnecessary questions for users can lead to a more streamlined and efficient component.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to